I've a static stl map in a C++ class and have another static member function to return a constant pointer to the object in the map. This map is common to all objects in the class.
Only problem is, I need to search this map and set it from another class, which is in a different .cpp/.h file, and I get unresolved external symbol when I try and compile them in vs2010. The methods are defined in the Timestream class as
static void setRoomList(std::map<std::string, RoomDescription> rl);
static RoomDescription * getRoom(std::string ref);
both functions are public, so there should be no access issues. These functions are defined as normal in the Timestream.cpp file i.e.,
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;
}
I'm trying to call this like
RoomDescription *r =Timestream::getRoom("Bathroom")
from the other class. Other posts on the web seem to talk about using extern, but I'm not sure about that. I don't see why this should be any different to calling any other member function from a different class?
Thanks, James
EDIT: Yes, I've declared
std::map<std::string, RoomDescription> roomList;
at the top of the Timestream.cpp file. In the header it's defined as
static std::map<std::string, RoomDescription> roomList;
I've included the header of RoomDescription in the header of the class I'm trying to call these methods from.
The error I get is this
Import.obj : error LNK2019: unresolved external symbol "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) referenced in function "public: int __thiscall Import::getRoomData(void)" (?getRoomData@Import@@QAEHXZ)
Timestream.obj : error LNK2001: unresolved external symbol "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)