我在 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")
来自其他班级。网络上的其他帖子似乎谈论使用 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:无法解析的外部符号“私有:静态类 std::map,类 std::allocator >,类 RoomDescription,struct std::less,类 std::allocator >>,类 std::allocator,类 std::allocator > const ,类 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?$分配器@D@2@@std@@@2@V?$分配器@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$分配器@D@2@@std@@VRoomDescription@@@std@@@2@@std@ @A)