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.
相关文章