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

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

php - facebook and github login HWIOAuthBundle and FOSUserBundle in Symfony2.1 -

hadoop - Sqoop installation export and import from postgresql -