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++?
for use of static variable in class, in first you must give a value generaly (no localy) to your static variable (initialize) then you can accessing a static member in class :
You need add a line:
That would define you a storage. Definition of static in class is similar to "extern" -- it provides symbol but does not create it. ie
foo.h
foo.cpp
You must add the following line in the implementation file:
This is required so the compiler has a place for the static variable.
It's the correct syntax, however,
Foo::bar
must be defined separately, outside of the header. In one of your.cpp
files, say this: