Sunday, 2 November 2014

Undefined reference to static variable in c++

Suppose i have a code which need to reference static variable
so we have following code in trial.h
class Trial
{
static int v;
void setCode();
}

in trail.cc
#include "trial.h"
void Trial::setCode()
{
v=1;
}

This above code will give undefined reference to Trial::v error. To remove this error either remove static word from v in .h file or if it is necessary to have static word. then do as following.
in trial.cc
#include "trial.h"
int Trial::v;
void Trial::setCode()
{
v=1;
}
now your error is gone . happy coding

No comments:

Post a Comment