Archived

This topic is now archived and is closed to further replies.

emileej

static member functions using static member variables

Recommended Posts

This is my scenario:
class CMyClass{
private:
    static int var;
public:
    static int GetVar();
};

int CMyClass::GetVar(){
    return var;
}
I get no compiler errors, but the linker complains "Undefined reference to ''CMyClass::var''". I simply dont get it.

Share this post


Link to post
Share on other sites
You''ll need to actually define your variable. At the moment, it is only declared. To define it, somewhere outside of your class, put int CMyClass::var; , or better, give it an initial value like int CMyClass::var = 0; Typically, assuming you have a .cpp file and a .h file, this line will go in the .cpp file. I typically put them at the top, before all the function declarations.


int Agony() { return *((int*)0); }    Mwahaha... >8)

Share this post


Link to post
Share on other sites