C ++“error:passing'const std :: map< int,std :: basic_string< char> >”作为“这个”的参数...“ [英] C++ "error: passing 'const std::map<int, std::basic_string<char> >' as 'this' argument of ..."

查看:484
本文介绍了C ++“error:passing'const std :: map< int,std :: basic_string< char> >”作为“这个”的参数...“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码(简单摘录):

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.cpp:

color::color() {
}

color::colorType color::getColorType() {
    return cColortype;
}

void color::setColorType(colorType cColortype) {
    this->cColortype = cColortype;
}

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

我收到以下错误:


color.cpp:16:29:error:将'const std :: map>'作为'std :: map< _Key,_Tp,_Compare,_Alloc > :: mapped_type&安培; std :: map< _Key,_Tp,_Compare,_Alloc> :: operator [](std :: map< _Key,_Tp,_Compare,_Alloc> :: key_type&& _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]

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]

错误是指返回颜色[cColortype];在getColorText中。

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

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

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.

任何帮助都赞赏。 p>

Any help is appreciated.

推荐答案

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

问题是您将功能标记为常量 std :: map 上的运算符[] 标记为非常量,不能在常量函数中使用喜欢这个。您需要手动使用 std :: map :: find (或其他机制)来搜索输入类型并处理未找到的情况。

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.

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

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 ++“error:passing'const std :: map&lt; int,std :: basic_string&lt; char&gt; &GT;”作为“这个”的参数...“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆