0

unordered_set独自のタイプの を作成しましたstruct。このセットにがあり、 が指す のメンバー ( )iteratorをインクリメントしたいと考えています。ただし、コンパイラは次のメッセージを表示します。countstructiterator

main.cpp:61:18: error: increment of member ‘SentimentWord::count’ in read-only object

どうすればこれを修正できますか?

これが私のコードです:

#include <fstream>
#include <iostream>
#include <cstdlib> 
#include <string>
#include <unordered_set>


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 &sw) const;
};

bool operator == (SentimentWord const &lhs, SentimentWord const &rhs);



int main(int argc, char **argv){


  ifstream fin;
  int totalWords = 0;
  unordered_set<SentimentWord, SentimentWordHash> positiveWords;
  unordered_set<SentimentWord, SentimentWordHash> 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<SentimentWord, SentimentWordHash>::iterator iter;


  fin.open("041.html");
  while(!fin.eof()){
    totalWords++;
    fin >> line;
    temp.word = line;
    iter = positiveWords.find(temp);
    if(iter != positiveWords.end()){
      iter->count++;
    }
  }


  for(iter = positiveWords.begin(); iter != positiveWords.end(); ++iter){
    if(iter->count != 0){
      cout << iter->word << endl;
    }
  }


  return 0;

}


size_t SentimentWordHash::operator () (const SentimentWord &sw) const {
  return hash<string>()(sw.word);
}


bool operator == (SentimentWord const &lhs, SentimentWord const &rhs){
  if(lhs.word.compare(rhs.word) == 0){
    return true;
  }
  return false;
} 

どんな助けでも大歓迎です!

4

3 に答える 3