【问题标题】:C++ "error: passing 'const std::map<int, std::basic_string<char> >' as 'this' argument of ..."C++“错误:将'const std::map<int, std::basic_string<char>>'作为...的'this'参数传递”
【发布时间】:2013-11-29 02:57:35
【问题描述】:

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

颜色.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& std::map::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]

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

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

我通常非常擅长调试/故障排除,但错误消息太复杂以至于没有多大帮助。

感谢任何帮助。

【问题讨论】:

  • 我的第一个建议是适当地使用 std::map::find 调用删除 color::getColorText() const 中的下标运算符。

标签: c++ map constants


【解决方案1】:
string color::getColorText() const {
    return colors[cColortype];
}

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

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

【讨论】:

  • std::map::at 完全符合要求。非常感谢。
  • 是的,std::map::at 是我真正需要的。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    首先:映射map&lt;int, string&gt; colors 必须是从 cColorType 到 string 而不是 int 的映射:

    map<cColorType, string> colors
    

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

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

    【讨论】:

    • 其实cColorType是一个变量名而不是一个类型。也许您的意思是 colorType 。这实际上不是必需的,因为 colorType 是一个映射到 int 的枚举,因此“map colors”签名工作得很好。我在帖子中遗漏的部分代码是一个访问器,它接受一个字符串作为参数并对颜色执行反向映射查找以获得相应的颜色类型。具有反向查找的地图无需以 2 种不同格式存储颜色设置。
    • 是的,您对 cColorType 的看法是正确的,我很抱歉。将 map 的第一个条目保留为“int”会起作用,但将其设置为 colorType 会更灵活:假设您(或其他人)将来会重写此代码,并且想要的不是 colorType 的枚举。该编码员不必更改地图的声明。您如何执行“反向地图查找”?我很想看看:)
    • 您的地图反向查找使用 find_if,它具有线性复杂度。如果您存储 2 个不同的属性,则 2 个 Get 函数将分别以恒定时间执行。并且由于两种颜色之间的匹配是完美的,您不需要反向查找。您可以编写两个映射来表示彼此的反函数。
    猜你喜欢
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多