Archived

This topic is now archived and is closed to further replies.

slyterence

Linker error with std::map...

Recommended Posts

Hi... I'm currently trying to implement a pluggable factory system like the one described in the "Why Pluggable Factories Rock My Multiplayer World" here on GameDev. Basically, there's an abstract base class called NetMessage_Maker that has a static std::map in it that stores a list of MessageID's and the pointers to the appropriate factories for each of them. Now, the problem I'm having is that when I try to extract the pointer from the map, I'm getting the following compiler error (Visual Studio .NET)... (Hope this gets the code formatting )
  
Poddle error LNK2001: unresolved external symbol "protected: static class std::map<int,class NetMessage_Maker *,struct std::less<int>,class std::allocator<struct std::Pair<int const ,class NetMessage_Maker *> > > NetMessage_Maker::m_Registry" (?m_Registry@NetMessage_Maker@@1V?$map@HPAVNetMessage_Maker@@U?$less@H@std@@V?$allocator@U?$pair@$$CBHPAVNetMessage_Maker@@@std@@@3@@std@@A)
  
This error only occurs when I add this line to the method : NetMessage_Maker *Maker = (*m_Registry.find(*msgID)).second; Can someone please tell me how to fix this error? Thanks $£¥ We scratch our eternal itch A twentieth century bitch We are grateful for Our Iron Lung [edited by - slyterence on May 3, 2003 6:46:16 AM]

Share this post


Link to post
Share on other sites
static variables need to be defined in a cpp file.

Say, you have

class Foo {
static float bar;
};

then you''ll also need

float Foo::bar = 2.4f;

in a cpp file. Linker will complain with "unresolved external symbol" if you don''t do that.

Share this post


Link to post
Share on other sites
Keep in mind that static class variables need to be declared in the header, and defined in the source file:


    
//myclass.h

class myclass
{
static int a;
};


//myclass.cpp

int myclass::a;


EDIT: civguy beat me to it. OMG HE IS TEH SPEEED RACAR!

How appropriate. You fight like a cow.

[edited by - sneftel on May 3, 2003 6:32:05 AM]

Share this post


Link to post
Share on other sites