C++ "error: passing 'const std::map<int, std::basic_string<char> >' as 'this' argument of ..."

c++, constants, dictionary

Solution

string color::getColorText() const {
    return colors[cColortype];
}

The issue is that you've marked the function as `const`. The `operator[]` on `std::map` is marked as non-const, and cannot be used in a const function like this. You need to manually use `std::map::find` (or other mechanism) to search for the input type and handle the case where it's not found.

If you're using C++11, you can instead use `std::map::at`, which IS allowed to be used on a constant map, and throws an exception if the requested element is not present.

Problem

With the following code (excerpted for brevity): color.h: ``` class color { public: color(); enum colorType { black, blue, green, cyan, red, magenta, brown, lightgray, nocolor }; colorType getColorType(); void setColorType(colorType cColortype); string getColorText() const; private: colorType cColortype = nocolor; map<int, string> colors = { {black, "black"}, {blue, "blue"}, {green, "green"}, {cyan, "cyan"}, {red, "red"}, {magenta, "magenta"}, {brown, "brown"}, {lightgray, "lightgray"}, {nocolor, "nocolor"}}; }; ``` color.cpp: ``` color::color() { } color::colorType color::getColorType() { return cColortype; } void color::setColorType(colorType cColortype) { this->cColortype = cColortype; } string color::getColorText() const { return colors[cColortype]; } ``` I get the following error: color.cpp:16:29: error: passing 'const std::map >' as 'this' argument of 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = int; _Tp = std::basic_string; _Compare = std::less; _Alloc = std::allocator > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::basic_string; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]' discards qualifiers [-fpermissive] The error refers to "return colors[cColortype];" in getColorText. I'm writing this for a class project and I can get it to work for the sake of the assignment by removing the const declaration in the getColorText signature but I'm trying to learn/adopt good practices and adhere to the recommendation to use const for member functions that don't modify data so I want to know how to deal with this going forward. I'm usually really good at debugging/troubleshooting but the error message is so convoluted that it's not much help. Any help is appreciated.

Original source

Related problems