>' as 'this' argument ..." With the following code (excerpt for brevity): ...">

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

With the following code (excerpt 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 '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 am writing this for a class project and I can make it work for the purpose of deleting the const declaration in the getColorText signature, but I am trying to learn / accept the best practice and adhere to the recommendation to use const for member functions that do not modify the data, so I want to know how to deal with it.

Usually I am good at debugging / troubleshooting, but the error message is so confusing that it doesn't help much.

Any help is appreciated.

+6
source share
3 answers
 string color::getColorText() const { return colors[cColortype]; } 

The problem is that you marked the function as const . operator[] on std::map marked as non-constant and cannot be used in such a const function. You need to manually use std::map::find (or another mechanism) to search for the input type and handle the case when it is not found.

If you use C ++ 11, you can instead use std::map::at , which is allowed for use on a permanent map and throws an exception if the requested element is missing.

+26
source

The key is near the end: "discards qualifiers." getColorText is a member function of const , so colors is const . But map::operator[]() not const .

+5
source

First: the map map<int, string> colors should be a map from cColorType to a string instead of int:

 map<cColorType, string> colors 

Second: as some people have already said: map::operator[]() not const . The reason for this is because this operator returns a link that allows you to change its value.

Let me suggest the following solution: you can create a second private attribute: color in lowercase format. Therefore, you will have two Get functions (one for each color type) and one Set function (which will change two color attributes).

+2
source

Source: https://habr.com/ru/post/959020/


All Articles