Hi i am getting undefined reference error in the following code:
class Helloworld{
public:
static int x;
void foo();
};
void Helloworld::foo(){
Helloworld::x = 10;
};
I don't want a static
foo()
function. How can I access static
variable of a class in non-static
method of a class?
The code is correct, but incomplete. The class
Helloworld
has a declaration of its static data memberx
, but there is no definition of that data member. Somehwere in your source code you needor, if 0 isn't an appropriate initial value, add an initializer.
Well,
foo()
is not static in your class, and you do not need to make itstatic
in order to accessstatic
variables of your class.What you need to do is simply to provide a definition for your static member variable: