"undefined reference to" error when accessing a static variable inside member function
Feb 10, 2013 at 2:49pm UTC
I am modifying a set of static variables inside of the class's member function. The static variables are private. An example of what I'm doing is as below,
utilities.h
-----------
1 2 3 4 5 6 7 8
class utilities
{
private :
static int num_nodes;
public :
void parse_details(char * );
};
utilities.cpp
-------------
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "utlities.h"
void utilities::parse_details(char * filename)
{
...
...
...
for (int i=0; i<num_nodes; i++)
{
...
...
}
}
I get a compilation error in the function
void utilities::parse_details(char * filename)
which says: undefined reference to `utilities::num_nodes'
can someone give me some pointers on how to solve this?
compiler: g++
Feb 10, 2013 at 2:52pm UTC
You only declared the static member but did not define it. Include in the cpp file the following statement
int utilities::num_nodes;
Last edited on Feb 10, 2013 at 2:53pm UTC
Feb 10, 2013 at 2:56pm UTC
Thanks indeed, that worked!
Topic archived. No new replies allowed.