title: c++ Map [] operator not found
body: <pre><code>void Library::addKeywordsForItem(const Item* const item, int nKeywords, ...)
  {
      // the code in this function demonstrates how to handle a vararg in C++
  
      va_list     keywords;
      char        *keyword;
  
      va_start(keywords, nKeywords);
      for (int i = 0; i &lt; nKeywords; i++)
      {
          keyword = va_arg(keywords, char*);
          ((Item*)const_cast&lt;Item*&gt;(item))-&gt;addKeyword(string(keyword)); 
  
          ItemSet* itemSet = keywordItems[string(keyword)];
          if (itemSet == NULL)
          {
              itemSet = new ItemSet();
              keywordItems[keyword] = itemSet;
          }
          bool isNull = (itemSet == NULL) ? true : false;
          itemSet-&gt;insert(((Item*)const_cast&lt;Item*&gt;(item)));
      }
      va_end(keywords);
  }
  
  const ItemSet* Library::itemsForKeyword(const string&amp; keyword) const
  {
      return keywordItems[((string)const_cast&lt;string&amp;&gt;(keyword))];
  }
  </code></pre>
  
  <p>In the above code, first method work as expected. Second method doesn't, and shows an error @ "["</p>
  
  <blockquote>
    <p>no operator "[]" matches these operands operand types are: const
    StringToItemSetMap [ std::string ]</p>
  </blockquote>
  
  <p>StringToItemSetMap is just a typedef of map. I have tried different casts, as well as create a local string variable, but not luck. Even something like keywordItems[string("test")]; doesn't work in second method but works in first one. Is there something that I might have missed?</p>
  
  <p>Edit:</p>
  
  <pre><code>const ItemSet* Library::itemsForKeyword(const string&amp; keyword) const
  {
      std::map&lt;std::string, ItemSet*&gt;::const_iterator it = keywordItems.find(keyword);
      if (it != keywordItems.end())
      {
          return it-&gt;second;
      }
      return NULL;
  }
  </code></pre>
  
  <p>As was pointed out in out of the answers, the problem was because of second method being const while map::operator[] is not.</p>