c++ member function access private static variable?

Mars picture Mars · Mar 23, 2012 · Viewed 18.5k times · Source

Possible Duplicate:
What does it mean to have an undefined reference to a static member?

I don't know why I got error "undefined reference to `TT::i'" if I implement the function outside of class declaration? Here is the code..

class TT{
private:
    static int i;
public:
    void t();
};

void TT::t(){
    i=0;
}

Answer

Stuart Golodetz picture Stuart Golodetz · Mar 23, 2012

It's nothing to do with where you defined the function, it's that you didn't define the static member variable, you only declared it. You need to put its definition outside the class, e.g.

int TT::i;