Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am having this error and would be nice if someone could help.

In my class header file I have declared following:

C#
class CStudentClass
{
private:

    static int m_currentNumberOfStudents;


However when I try to use the above private variable in .cpp file I get an error saying:

VB
1>StudentClass.obj : error LNK2001: unresolved external symbol "private: static int CStudentClass::m_currentNumberOfStudents" (?m_currentNumberOfStudents@CStudentClass@@0HA)
1>c:\my documents\Projects\Student_Class\Debug\Student_Class.exe : fatal error LNK1120: 1 unresolved externals
1



What am I doing wrong? thanks

I tried referring to this variable in my .cpp file both using: m_currentNumberOfStudents and CStudentClass::m_currentNumberOfStudents but both seem to fail??
Posted
Updated 8-Dec-12 2:58am
v2
Comments
Sergey Alexandrovich Kryukov 9-Dec-12 2:04am    
Before considering the problem, it's good to think: why using static field at all. This should be done only in special cases. Usually, using such fields can be eliminated; and it's the best to avoid them.
--SA

1 solution

Static members have to be defined inside the .cpp file, something like this:
C++
int CStudentClass::m_currentNumberOfStudents = 0;


(This is in addition to the declaration inside the class.)
 
Share this answer
 
Comments
[no name] 8-Dec-12 9:26am    
OK I see but in this case if I have several CStudentClass objects, does not this mean each time I declare these objects, m_currentNumberOfStudents will be initialized to 0?? I want m_currentNumberOfStudents to be smth like a counter but I have an idea already how to do it.. for instance (quite easy) I will put int CStudentClass::m_currentNumberOfStudents = 0; in the constructor only in the case when m_currentNumberOfStudents is already zero?
Graham Breach 8-Dec-12 10:57am    
No - the line of code goes outside of any functions, and only initializes the variable once at program start-up.

If you want each instance of the class to have its own number of students, then use a non-static member variable instead and initialize it with the class constructor. Of course you can do both, but you would have to give them different names.

This stuff should be near the start of any decent C++ text book.
[no name] 8-Dec-12 16:24pm    
What if I want to keep a counter across different instances of the class?
Graham Breach 9-Dec-12 5:54am    
Then you use the static member variable and increase or decrease its value.
[no name] 9-Dec-12 6:44am    
Hi thanks but if I call int CStudentClass::m_currentNumberOfStudents = 0;
in the .cpp then won't this initialize m_currentNumberOfStudents to zero every time a different instance is created? and basically this way I will lose the counter value?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900