Could you help me understand why I can't increment the static variable? I face this: *error LNK2001: unresolved external symbol "private: static unsigned int Counter::m_curCounters" (?m_curCounters@Counter@@0IA)*
counter.cpp
#include "counter.h"
static unsigned int m_curCounters = 0;
Counter::Counter(const char* p){
...
m_curCounters++;
}
Counter::Counter(){
...
m_curCounters++;
}
I think whats happening here that you have m_curCounters
declared in counter.h
and that you create a new local static in the cpp.
But class statics need to be visible to the linker by defining them in the cpp as unsigned int Counter::m_curCounters = 0;
Now when you try to use the local static the compiler gives precedence to the class static which the linker cant find later on
I suppose you have m_curCounters
declared in Counter.
Then you need to define it as
unsigned int Counter::m_curCounters = 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With