Undefined reference for static member of a template class

Rupert Wood me@rupey.net
Wed Dec 12 07:06:00 GMT 2001


Devrim Erdem wrote:

> I have a template class like this :
:
>     static ObjType *mInstance ;
:
> The compiler (gcc 2.95.2-149) reports a undefined refence error
> during linkage. Any ideas ?

Since you didn't tell us, I'll assume that your error looks like this:

    Undefined                       first referenced
     symbol                             in file
    Mingleton<ClassA>::mInstance        /var/tmp/ccVAIFxr.o

When you declare a static member of a class, templated or not, you must
also define it: you must allocate some storage in one of your source
files so that there is a single, cross-project instantiation of the
static member. Try adding:

    ClassA* ClassB::mInstance;

to one of your sources at global (or matching namespace) scope.

Hope that helps,
Rup.



More information about the Gcc-help mailing list