C++“错误:将‘const std::map<int, std::basic_string<char> >’作为……的‘this’参数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20277671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:00:42  来源:igfitidea点击:

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

c++mapconst

提问by WXB13

With the following code (excerpted for brevity):

使用以下代码(为简洁起见摘录):

color.h:

颜色.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:

颜色.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]

color.cpp:16:29: 错误:将 'const std::map >' 作为 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp 的 'this' 参数传递, _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]' 丢弃限定符 [-fpermissive]

The error refers to "return colors[cColortype];" in getColorText.

错误是指“返回颜色[cColortype];” 在 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.

我正在为一个班级项目编写这个,我可以通过删除 getColorText 签名中的 const 声明来使其工作以便分配,但我正在尝试学习/采用良好做法并遵守使用 const 的建议对于不修改数据的成员函数,所以我想知道如何处理这个问题。

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.

任何帮助表示赞赏。

回答by Dave S

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

The issue is that you've marked the function as const. The operator[]on std::mapis 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.

问题是您已将该函数标记为const. 的operator[]std::map被标记为非const,并且不能在这样一个const函数一起使用。您需要手动使用std::map::find(或其他机制)来搜索输入类型并处理找不到的情况。

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.

如果您使用的是 C++11,则可以改用std::map::at,它允许在常量映射上使用,如果请求的元素不存在,则抛出异常。

回答by Pete Becker

The key is near the end: "discards qualifiers". getColorTextis a constmember function, so colorsis const. But map::operator[]()is not const.

关键是接近尾声:“丢弃限定符”。getColorTextconst成员函数,所以colors也是const。但map::operator[]()不是const

回答by Brainless

First : the map map<int, string> colorsmust be a map from cColorTypeto string instead of int :

首先:映射map<int, string> colors必须是从cColorType到 string 而不是 int的映射:

map<cColorType, string> 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.

第二:正如一些人已经回答:map::operator[]()不是const。原因是这个操作符返回一个引用,它允许你修改它的值。

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).

让我建议以下解决方案:您可以创建第二个私有属性:字符串格式的颜色。因此,您将有 2 个 Get 函数(每种颜色一个)和一个 Set 函数(将修改 2 个颜色属性)。