0

I have an std::map class member that I just want to index read in a class member function that is const. I'm not changing the map, but I still get an error. I do not seem to get an error if I read another class member, like an int, or if I iterate through that map.

here is the code:

class Test
{
public:
    Test()
    {
        table[1] = "one";
    }

    void printData() const
    {
        // valid
        std::cout << val << std::endl;
        
        // valid
        for ( auto entry : table ) std::cout << entry.second << std::endl;
        
        // error:
        // passing 'const std::map<int, std::__cxx11::basic_string<char> >' as 'this' argument discards qualifiers [-fpermissive]
        std::cout << table[1] << std::endl;
    }

    int val;
    std::map<int, std::string> table;

};
chalbers
  • 1
  • 2

0 Answers0