Click to See Complete Forum and Search --> : Static class member variable, link error, I can't understand


Will Rothwell
May 5th, 1999, 02:40 AM
Hi,

I need to access a common value (the number of administrators on the system) by different instance of a class. So I changed an existing protected class member from int m_nAdminCount to static int m_nAdminCount. It compiled OK but with 2 linking errors :

PKI.obj : error LNK2001: unresolved external symbol "protected: static int CPKI::m_nAdminCount" (?m_nAdminCount@CPKI@@1HA)
.\DEBUG/Screener.exe : fatal error LNK1120: 1 unresolved externals

Can someone please tell me what is wrong and how to define a static member variable. Thanks.

Will

Franky Braem
May 5th, 1999, 04:17 AM
Did you initialise your static member ?

You have to do this in your source-file like this :
int CPKI::m_nAdminCount = 0;

Dave Lorde
May 5th, 1999, 04:55 AM
Yes, you must define the static member in file scope, but note that you don't need to initialise it to zero, static members are guaranteed to be initialized to zero with the appropriate value type, by default.

Dave