C++ クラスに静的 stl マップがあり、マップ内のオブジェクトへの定数ポインターを返す別の静的メンバー関数があります。このマップは、クラス内のすべてのオブジェクトに共通です。
唯一の問題は、このマップを検索して、別の .cpp/.h ファイルにある別のクラスから設定する必要があることです。vs2010 でそれらをコンパイルしようとすると、未解決の外部シンボルが取得されます。メソッドは Timestream クラスで次のように定義されています。
static void setRoomList(std::map<std::string, RoomDescription> rl);
static RoomDescription * getRoom(std::string ref);
どちらの関数も公開されているため、アクセスの問題はありません。これらの関数は、Timestream.cpp ファイルで通常どおり定義されています。つまり、
RoomDescription * Timestream::getRoom(std::string ref)
{
std::map<std::string, RoomDescription>::iterator cIter= roomList.find(ref);
if(cIter!=roomList.end())
return &(cIter->second);
return NULL;
}
私はこれを次のように呼ぼうとしています
RoomDescription *r =Timestream::getRoom("Bathroom")
他のクラスから。Web 上の他の投稿では、extern の使用について話しているようですが、それについてはよくわかりません。これが、別のクラスから他のメンバー関数を呼び出すのとなぜ違うのかわかりませんか?
ありがとう、ジェームズ
編集:はい、私は宣言しました
std::map<std::string, RoomDescription> roomList;
Timestream.cpp ファイルの上部にあります。ヘッダーでは、次のように定義されています
static std::map<std::string, RoomDescription> roomList;
これらのメソッドを呼び出そうとしているクラスのヘッダーに RoomDescription のヘッダーを含めました。
私が得るエラーはこれです
Import.obj : エラー LNK2019: 未解決の外部シンボル "public: static void __cdecl Timestream::setRoomList(class std::map,class std::allocator >,class RoomDescription,struct std::less,class std::allocator > > ,class std::allocator,class std::allocator > const ,class RoomDescription> > >)" (?setRoomList@Timestream@@SAXV?$map@V?$basic_string@DU?$char_traits@D@std@@V ?$allocator@D@2@@std@@VRoomDescription@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2 @V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@VRoomDescription@@@std@@@2 @@std@@@Z) 関数 "public: int __thiscall Import::getRoomData(void)" で参照 (?getRoomData@Import@@QAEHXZ)
Timestream.obj : エラー LNK2001: 未解決の外部シンボル "private: static class std::map,class std::allocator >,class RoomDescription,struct std::less,class std::allocator > >,class std::allocator, class std::allocator > const ,class RoomDescription> > > Timestream::roomList" (?roomList@Timestream@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@ D@2@@std@@VRoomDescription@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$ allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@VRoomDescription@@@std@@@2@@std@ @A)