C++“错误:传递'const std::map<int, std::basic_string<char>>'作为......的“这个"论点

C++ quot;error: passing #39;const std::maplt;int, std::basic_stringlt;chargt; gt;#39; as #39;this#39; argument of ...quot;(C++“错误:传递const std::maplt;int, std::basic_stringlt;chargt;gt;作为......的“这个论点)
本文介绍了C++“错误:传递'const std::map<int, std::basic_string<char>>'作为......的“这个"论点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With the following code (excerpted for brevity):

颜色.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"}};
};

颜色.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: 错误:将 'const std::map >' 作为 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& 的 'this' 参数传递std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = int;_Tp = std::basic_string;_比较 = 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.

感谢任何帮助.

推荐答案

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

问题是您已将函数标记为 const.std::map 上的 operator[] 被标记为非常量,并且不能在这样的 const 函数中使用.您需要手动使用 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++“错误:传递'const std::map&lt;int, std::basic_string&lt;char&gt;&gt;'作为......的“这个"论点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)