Archived

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

static member functions using static member variables

This topic is 5011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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