"undefined reference to" error when accessing a static variable inside member function

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++
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
Thanks indeed, that worked!

Topic archived. No new replies allowed.