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
  • 28,217
  • 50
  • 150
  • 240

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
  • 44,644
  • 9
  • 83
  • 100