c++ - Undefined reference to static class member -
can explain why following code won't compile? @ least on g++ 4.2.4.
and more interesting, why compile when cast member int?
#include <vector> class foo { public: static const int member = 1; }; int main(){ vector<int> v; v.push_back( foo::member ); // undefined reference `foo::member' v.push_back( (int) foo::member ); // ok return 0; }
you need define static member somewhere (after class definition). try this:
class foo { /* ... */ }; const int foo::member; int main() { /* ... */ }
that should rid of undefined reference.
Comments
Post a Comment