Thursday, October 02, 2008

A Trivial Non-Trivial Problem (C++ Linking Error)

MESSAGE: error LNK2001: unresolved external symbol "private: static class .....

This error message occurred during link phase (in Visual C++) stopped me from moving on for a few hours. I just got it solved.

The situation is like this: I am going to use static member attribute in a class to reduce the effort to create more than one instance of a certain object A in class B. I declared it like this:

In file B.h

class B {

static A *varA;

};

Then the compiler complains unresolved symbol like the message above.

But it is there!

I tried so many ways to work it out until just minutes ago. I found that it seems non-primitive types been used in static way need be declared separately again so it is actually instantiated:

In File B.cpp

#include

A * B::varA = NULL;

This solves the problem.
The problem is not hard. It's just I am not knowledgeable of C++.

No comments: