I have a few static unordered_maps in my application that I would like to be able to initialize on start up. They are not part of a class. In my initial setup, the header and source file were as follows:
namespace Game
{
namespace Elements
{
enum Element
{
Air = 1,
Dark = 2,
Earth = 4,
Elementless = 8,
Fire = 16,
Ice = 32,
Light = 64,
SpaceTime = 128,
Thunder = 256,
Water = 512,
};
static boost::unordered_map<Element, std::string> ElementNameMap;
static boost::unordered_map<std::string, Element> NameElementMap
}
}
And the source file:
#include "Elements.h"
using namespace std;
using namespace Game::Elements;
boost::unordered_map<Element, std::string> ElementNameMap = boost::assign::map_list_of
(Element::Air, string("Air"))
(Element::Dark, string("Dark"))
(Element::Earth, string("Earth"))
(Element::Elementless, string("Elementless"))
(Element::Fire, string("Fire"))
(Element::Ice, string("Ice"))
(Element::Light, string("Light"))
(Element::SpaceTime, string("SpaceTime"))
(Element::Thunder, string("Thunder"))
(Element::Water, string("Water"))
;
boost::unordered_map<std::string, Element> NameElementMap = boost::assign::map_list_of
(string("Air"), Element::Air)
(string("Dark"), Element::Dark)
(string("Earth"), Element::Earth)
(string("Elementless"), Element::Elementless)
(string("Fire"), Element::Fire)
(string("Ice"), Element::Ice)
(string("Light"), Element::Light)
(string("SpaceTime"), Element::SpaceTime)
(string("Thunder"), Element::Thunder)
(string("Water"), Element::Water)
;
However, whenever I tried doing this then tried to do a lookup on the maps (i.e. Game::Elements::NameElementMap[std::string(“Air”)] ) it always returns an empty string, and the size in that use context is 0.
I tried moving the initialization into the header file (i.e. in the header file putting
static boost::unordered_map<Element, std::string> ElementNameMap = boost::assign::map_list_of
(Element::Air, string("Air"))
(Element::Dark, string("Dark"))
(Element::Earth, string("Earth"))
(Element::Elementless, string("Elementless"))
(Element::Fire, string("Fire"))
(Element::Ice, string("Ice"))
(Element::Light, string("Light"))
(Element::SpaceTime, string("SpaceTime"))
(Element::Thunder, string("Thunder"))
(Element::Water, string("Water"))
;
static boost::unordered_map<std::string, Element> NameElementMap = boost::assign::map_list_of
(string("Air"), Element::Air)
(string("Dark"), Element::Dark)
(string("Earth"), Element::Earth)
(string("Elementless"), Element::Elementless)
(string("Fire"), Element::Fire)
(string("Ice"), Element::Ice)
(string("Light"), Element::Light)
(string("SpaceTime"), Element::SpaceTime)
(string("Thunder"), Element::Thunder)
(string("Water"), Element::Water)
;
but the compiler then complained about having no default constructor. What am I doing wrong?
thanks in advance
You have to put the definitions in the correct namespace. Otherwise you are defining two new maps in the global namespace, instead of the ones in
Game::Elements:(Also enums don’t create a new namespace, so it’s just
Air, notElement::Air.)