title: How to make an iterator to a read-only object writable (in C++)
body: <p>I've created a <code>unordered_set</code> of my own type of <code>struct</code>.  I have an <code>iterator</code> to this set and would like to increment a member (<code>count</code>) of the <code>struct</code> that the <code>iterator</code> points to.  However, the compiler complains with the following message:</p>
  
  <p><code>main.cpp:61:18: error: increment of member ‘SentimentWord::count’ in read-only object</code></p>
  
  <p>How can I fix this?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt; 
  #include &lt;string&gt;
  #include &lt;unordered_set&gt;
  
  
  using namespace std;
  
  
  struct SentimentWord {
    string word;
    int count;
  };
  
  
  //hash function and equality definition - needed to used unordered_set with type   SentimentWord
  struct SentimentWordHash {
    size_t operator () (const SentimentWord &amp;sw) const;
  };
  
  bool operator == (SentimentWord const &amp;lhs, SentimentWord const &amp;rhs);
  
  
  
  int main(int argc, char **argv){
  
  
    ifstream fin;
    int totalWords = 0;
    unordered_set&lt;SentimentWord, SentimentWordHash&gt; positiveWords;
    unordered_set&lt;SentimentWord, SentimentWordHash&gt; negativeWords;
  
  
    //needed for reading in sentiment words
    string line;
    SentimentWord temp;
    temp.count = 0;
  
  
    fin.open("positive_words.txt");
    while(!fin.eof()){
      getline(fin, line);
      temp.word = line;
      positiveWords.insert(temp);
    }
    fin.close();
  
  
    //needed for reading in input file
    unordered_set&lt;SentimentWord, SentimentWordHash&gt;::iterator iter;
  
  
    fin.open("041.html");
    while(!fin.eof()){
      totalWords++;
      fin &gt;&gt; line;
      temp.word = line;
      iter = positiveWords.find(temp);
      if(iter != positiveWords.end()){
        iter-&gt;count++;
      }
    }
  
  
    for(iter = positiveWords.begin(); iter != positiveWords.end(); ++iter){
      if(iter-&gt;count != 0){
        cout &lt;&lt; iter-&gt;word &lt;&lt; endl;
      }
    }
  
  
    return 0;
  
  }
  
  
  size_t SentimentWordHash::operator () (const SentimentWord &amp;sw) const {
    return hash&lt;string&gt;()(sw.word);
  }
  
  
  bool operator == (SentimentWord const &amp;lhs, SentimentWord const &amp;rhs){
    if(lhs.word.compare(rhs.word) == 0){
      return true;
    }
    return false;
  } 
  </code></pre>
  
  <p>Any help is greatly appreciated!</p>