I want to initialize a std::map in my_cpp.h
header file:
std::map<std::string, double> my_map;
my_map["name1"] = 0;
my_map["name2"] = 0;
But there was a compile error showed up:
error: ‘my_map’ does not name a type
Can someone explain why this not work for a C++ newbie? Thanks
You can't initialize a map in a .h
file like that. Those assignment statements need to be inside a function/method instead.
Otherwise, initialize the map directly in its declaration, eg
std::map<std::string, double> my_map = {
{"name1", 0.0},
{"name2", 0.0}
};