I've declared a public static member to keep the total count of the instances of my class. The code is as follows:
class Hello {
public:
static int myCount;
void test(){
//do nothing
};
Hello(){
Hello::myCount += 1;
};
~Hello() {
Hello::myCount -= 1;
}
};
int main(int argc, const char * argv[]) {
// insert code here...
Hello *p1 = new Hello();p1->test();
Hello *p2 = new Hello();p2->test();
cout << Hello::myCount;
return 0;
}
However, I when compile, it says:
Undefined symbols for architecture x86_64:
"Hello::myCount", referenced from:
_main in main.o
Hello::Hello() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I don't know where I'm wrong. It's been years from the last time I worked with c++, so could you please suggest a solution? Thank you.
Aucun commentaire:
Enregistrer un commentaire