c++ - Unable to access static member -> unresolved external symbol i -


this question has answer here:

class test { private:     static int i; public:      static void foo()     {         = 10;     }     static int geti(){ return i; } };  int _tmain(int argc, _tchar* argv[]) {        test::foo();     std::cout << test::geti();     return 0; } 

this simple test, think wrong static use in program. because "unresolved external symbol i". why ?

you have define static member variables outside class definition:

int test::i=10; //or value or definition do. 

in class declaring, not defining. without definition, linker not able find ans show unresolved external symbols.

static member variables in class requires special treatment because of one definition rule , compilation model of c++.


Comments

Popular posts from this blog

C# random value from dictionary and tuple -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -

cgi - How do I interpret URLs without extension as files rather than missing directories in nginx? -