저는 게임 진행 중 모듈화를 시도하는 중입니다. 나는 room_t 객체들 (room_t rooms [])에있는 모든 room_t 객체들의 단일 배열을 선언하고 world.cpp에 저장하고 다른 파일들로부터 그것을 호출 할 수 있기를 원한다.참조 다른 파일의 C++ struct 객체?
아래의 잘린 코드는 작동하지 않지만 내가 얻은 것까지입니다. extern을 사용해야하지만 제대로 작동하는 메서드를 찾을 수 없었습니다. 헤더 파일에서 배열을 선언하면 중복 객체 오류가 발생합니다 (각 파일이 world.h를 호출 할 때마다 가정합니다).
MAIN.CPP
#include <iostream>
#include "world.h"
int main()
{
int currentLocation = 0;
cout << "Room: " << rooms[currentLocation].name << "\n";
// error: 'rooms' was not declared in this scope
cout << rooms[currentLocation].desc << "\n";
return 0;
}
world.h
#include "world.h"
room_t rooms[] = {
{"Bedroom", "There is a bed in here.", {-1,1,2,-1} },
{"Kitchen", "Knives! Knives everywhere!", {0,-1,3,-1} },
{"Hallway North", "A long corridor.",{-1,-1,-1,0} },
{"Hallway South", "A long corridor.",{-1,-1,-1,1} }
};
통근이 당신의 친구입니다 – Macmade