Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I initialize a static const vector that is a class member in c++11?

I need to initialize a static const vector that is a class member.

I have tried:

static const vector<string> vr ({"2", "3", "4"});  

static const vector<string> vr = {"2", "3", "4"}; 

static const vector<string> vr {"2", "3", "4"};

However, none of these work.

I am using Eclipse with mingw. (I have enabled c++11)

like image 604
user1740587 Avatar asked Oct 12 '12 09:10

user1740587


2 Answers

Static variable initialization is done outside of the class, like this:

class Example
{
    static const vector<string> vr;
    // ...
};

const vector<string> Example :: vr ({"hello", "world"});
like image 77
Tomislav Dyulgerov Avatar answered Nov 18 '22 20:11

Tomislav Dyulgerov


Declare your static members in your class definition, but define them outside.

class MyClass {
public:
    // declaration
    static const std::vector<std::string> vec;
};

// definition
const std::vector<std::string> MyClass::vec = ...;

The exception to this is integral types,

class MyClass {
public:
    // declaration and definition
    static const int MAX_BURRITOS = 5;
};
like image 13
Dietrich Epp Avatar answered Nov 18 '22 20:11

Dietrich Epp