Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: How to declare an empty private static vector inside a class? [duplicate]

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!

like image 368
Tony Bogdanov Avatar asked Sep 10 '25 23:09

Tony Bogdanov


2 Answers

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;
like image 50
Caribou Avatar answered Sep 12 '25 13:09

Caribou


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.

like image 33
Yakov Galka Avatar answered Sep 12 '25 12:09

Yakov Galka