title: C++ "error: passing 'const std::map<int, std::basic_string<char> >' as 'this' argument of ..."
body: <p>With the following code (excerpted for brevity):</p>
  
  <p>color.h:</p>
  
  <pre><code>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&lt;int, string&gt; colors = {
          {black, "black"},
          {blue, "blue"},
          {green, "green"},
          {cyan, "cyan"},
          {red, "red"},
          {magenta, "magenta"},
          {brown, "brown"},
          {lightgray, "lightgray"},
          {nocolor, "nocolor"}};
  };
  </code></pre>
  
  <p>color.cpp:</p>
  
  <pre><code>color::color() {
  }
  
  color::colorType color::getColorType() {
      return cColortype;
  }
  
  void color::setColorType(colorType cColortype) {
      this-&gt;cColortype = cColortype;
  }
  
  string color::getColorText() const {
      return colors[cColortype];
  }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>color.cpp:16:29: error: passing 'const std::map >' as 'this' argument of 'std::map&lt;_Key, _Tp, _Compare, _Alloc>::mapped_type&amp; std::map&lt;_Key, _Tp, _Compare, _Alloc>::operator[](std::map&lt;_Key, _Tp, _Compare, _Alloc>::key_type&amp;&amp;) [with _Key = int; _Tp = std::basic_string; _Compare = std::less; _Alloc = std::allocator > >; std::map&lt;_Key, _Tp, _Compare, _Alloc>::mapped_type = std::basic_string; std::map&lt;_Key, _Tp, _Compare, _Alloc>::key_type = int]' discards qualifiers [-fpermissive]</p>
  </blockquote>
  
  <p>The error refers to "return colors[cColortype];" in getColorText.</p>
  
  <p>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.</p>
  
  <p>I'm usually really good at debugging/troubleshooting but the error message is so convoluted that it's not much help.</p>
  
  <p>Any help is appreciated.</p>