Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
977 views
in Technique[技术] by (71.8m points)

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

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::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]

The error refers to "return colors[cColortype];" in 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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
string color::getColorText() const {
    return colors[cColortype];
}

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...