Archived

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

Linking error for class in same file?

This topic is 6361 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

plug.obj : error LNK2001: unresolved external symbol "protected: static class std::map,class std::allocator > BaseMessageClass::Registry" (?Registry@Ba seMessageClass@@1V?$map@EPAVBaseMessageClass@@U?$less@E@std@@V?$allocator@PAVBaseMessageClass@@@3@@std@@A) Error occurs in my mini test app. The project is a single file, all the STL includes necessary are there, using namespace std; ,check that too. Why would I get a linking error on something like this? Here is the class it''s referring to:
    


class BaseMessageClass
{
public:
	virtual void MessageData(ByteArray &Data)=0;
protected:
	void RegisterID(MESSAGEID messageid)
	{
		Registry.insert(RegistryMap::value_type((unsigned char)messageid,this));
	}
	typedef map<unsigned char, BaseMessageClass*> RegistryMap;

	static RegistryMap		Registry;
};

    
Any pointers would be greatly appreciated. Thanks Chris Brodie

Share this post


Link to post
Share on other sites
I''ve found the exact section of my code that is causing the link failure

In a derived class difined as follows:
    
class CMessageSwitch : public BaseMessageClass
{
public:
void Switch(ByteArray &Data)
{
BaseMessageClass *TheMessageHandler = (*Registry.find((unsigned char)Data[0])).second;//<<<<<<<<<<<Here

TheMessageHandler->MessageData(Data);
}
private:
void MessageData(ByteArray &Data) {};

};

Thats the bit that causes the problem but only once I try to ''new'' this particular class, and only on that particular line(commenting the line make the linking probs go away)

hrmm... still thinking...

gimp

Share this post


Link to post
Share on other sites
Ok, it seems to be something to do with accessing the static map from member functions?

The following code is interesting in that from the constructor I can access it fine but not from a member functions(I''ve even tried making the member fucntion static but it makes no difference...

    
class CBaseMessage
{
public:
CBaseMessage(MESSAGEID messageid)
{
//Registry.insert(RegistryMap::value_type((unsigned char)messageid,this));
int i=Registry.size();
}

static void Switch(ByteArray &Data)
{
unsigned char messageid = Data[0];
//CBaseMessage *TheMessageHandler = (*Registry.find(messageid)).second;
//TheMessageHandler->ProcessData(Data);
int i=Registry.size();
}

protected:

typedef map<unsigned char, CBaseMessage*> RegistryMap;
static RegistryMap Registry;
virtual void ProcessData(ByteArray &Data)=0;

};

Share this post


Link to post
Share on other sites
When you have a static varriable in a class, it is similar to having extern on a global variable. It is like you are declaring that this variable exists somewhere.

You need a line like this in a cpp file.

    
RegistryMap BaseMessageClass::Registry;


Hope this fixes your problem. I think you are almost there.

Later,
Eck

Share this post


Link to post
Share on other sites