4

Possible Duplicate:
Initializing private static members

This is really driving me crazy, I want to declare a static private vector inside a class I am going to use as a shared memory.

My vector declaration goes like this:

private: static vector< pair< string, bool > > flags;

This is done inside a class, but how can I initialize it as empty vector? The best would be if the init would be in the class itself, because I need to use it in many places. The other option would be in main() but nothing more.

I have setFlag() and getFlag() methods that work with the vector, but it gives me all kinds of linker errors, because there is only declaration, no definition!

Community
  • 1
  • 1
Tony Bogdanov
  • 7,436
  • 10
  • 49
  • 80

2 Answers2

7

I have setFlag() and getFlag() methods that work with the vector, but it gives me all kinds of linker errors, because there is only declaration, no definition!

you need to initialise it in the class implementation file (or another source file):

vector< pair< string, bool > > MyClass::flags;
Caribou
  • 2,070
  • 13
  • 29
2

You have to add a definition in the file that implements YourClass:

vector< pair< string, bool > > YourClass::flags;

This line will call the default constructor, which initializes an empty vector.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220