1

I am trying to have a enum struct mapping to guarante a default value to a config value if it does not exsist and to guarante only access to "real" config values.(No std::string get)

So the Header looks like this:

enum ConfigValues
{
    LOG_LEVEL,
};

class Config
{
public:
    std::string get(const ConfigValues& key);
private:
    struct ConfigMapping
    {
        std::string configKeyString;
        std::string defaultValue;
    };

    const static std::map<ConfigValues, ConfigMapping> m_mapping;
}

And the cpp contains this:

const std::map<ConfigValues, Config::ConfigMapping> Config::m_mapping=
{
    {LOG_LEVEL, { "logLevel", "5" } },
};
std::string Config::get(const ConfigValues& key)
{
    std::string key = m_mapping[key].configKeyString; // <-- Does not work
}

But i cant access the map.

bemeyer
  • 6,154
  • 4
  • 36
  • 86
  • 1
    In Config::get(), both parameter and local are named key. Is that a typo in the example code or the actual problem? Maybe the actual compiler message might make things clearer. – stefaanv Jul 28 '15 at 08:09
  • See also http://stackoverflow.com/questions/5134614/c-const-map-element-access – stefaanv Jul 28 '15 at 08:21

1 Answers1

6

operator[] is not const for a std::map.

Use at instead.

Before C++11 :

If you are not using C++11, you have to use find which has a const version.

Telokis
  • 3,399
  • 14
  • 36