1

Possible Duplicate:
static member variable in a class
What is an undefined reference/unresolved external symbol error and how do I fix it?

class test_struct {
  static int number;
 public:
  static void set() {
    number = 42;
  }
};

int main() {
  test_struct::set();
}

The error is:

[...]Temp\ccGGxEAz.o:test.cpp:(.text.startup+0xf): undefined reference to `test_struct::number'
collect2.exe: error: ld returned 1 exit status
Community
  • 1
  • 1
rsk82
  • 26,257
  • 48
  • 141
  • 229

1 Answers1

5

You need to define static member test_struct::number in a source code(.cpp), before using it:

class test_struct {
  static int number;
 public:
  static void set() {
    number = 42;
  }
};

int test_struct::number = 0;
billz
  • 43,318
  • 8
  • 77
  • 98