I have the following code :I have the following code:
//MyClass.h
class MyClass {
public:
typedef std::map<std::string, int> OpMap;
static OpMap opMap_;
//methods
};
//MyClass.cpp
//Init opMap_
MyClass::opMap_["x"] = 1; //compilation error
How can I any case initialize (static) opMap_?
As you are using VS2010, you need to initialize your static member in MyClass.cpp, in front of any other member function definitions. call
MyClass::InitMap()
if you want to initializeopMap_
.MyClass.h
MyClass.cpp
If you're using C++11, you could use initializer lists:
If you don't have access to a compiler that supports the C++11 standard, you could do the following: