C++ “error: passing 'const std::map >' as 'this' argument of …”

后端 未结 3 1936
清酒与你
清酒与你 2020-12-03 18:02

With the following code (excerpted for brevity):

color.h:

class color {
public:
    color();

    enum colorType {
        black, blue, green, cyan,          


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 18:18

    First : the map map colors must be a map from cColorType to string instead of int :

    map colors
    

    Second : As some people already answered : map::operator[]() is not const. The reason for that is that this operator returns a reference, which allows you to modify its value.

    Let me suggest the following solution : You can create a second private attribute : the color in string format. Therefore you'll have 2 Get functions (one for each type of color), and one Set function (which will modify the 2 color attributes).

提交回复
热议问题