Duplicate: C++: undefined reference to static class member
If I have a class/struct like this
// header file class Foo { public: static int bar; int baz; int adder(); }; // implementation int Foo::adder() { return baz + bar; }
This doesn't work. I get an "undefined reference to `Foo::bar'" error. How do I access static class variables in C++?
You must add the following line in the implementation file:
int Foo::bar = you_initial_value_here;
This is required so the compiler has a place for the static variable.
1.4m articles
1.4m replys
5 comments
56.9k users