0
class Test
{
private:
    static int i;
public: 
    static void foo()
    {
        i = 10;
    }
    static int geti(){ return i; }
};

int _tmain(int argc, _TCHAR* argv[])
{   
    Test::foo();
    std::cout << Test::geti();
    return 0;
}

This is a simple test, I think I am wrong about static use in my program. Because I always get "Unresolved external symbol i". Why is this ?

user3431664
  • 51
  • 1
  • 10

1 Answers1

1

You have to define static member variables outside class definition:

int Test::i=10; //or any value or just the definition will do.

In class you are just declaring, not defining. Without a definition, linker will not be able to find it ans will show unresolved external symbols.

static member variables in a class requires this special treatment because of One Definition Rule and compilation model of C++.

Rakib
  • 7,435
  • 7
  • 29
  • 45