ошибка: недопустимый декларатор перед токеном '&'

Я пытался написать программу TextQuery, которая позволяет пользователю: 1. inputs a word
2. reads a file
3. prints out which lines the words appear and how many times the word appears on that line.

I created a class called "TextQuery" with 3 member functions:
1. "read_file" to read the file and return a reference to a vector
2. "find_word" to take the word needs to be searched
then returns a reference to a map< int, pair >
(the 1st 'int' is the line number, the 2nd 'int' is the number of times the word occurs on that line, the 'string' is the whole line)
3. "write_out" для записи результата.

Однако, когда я компилировал программу, я получил следующее сообщение:

/home/phongcao/C++/textquery_class_1.cc:21: error: invalid declarator before ‘&’ token


Мне просто интересно, как может ошибаться декларатор? Вот раздел определения класса:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <map>
#include <vector>
#include <string>

using namespace std;

class TextQuery {
public:
  vector<string> &read_file(ifstream &infile) const;
  map< int, pair<string, int> > &find_word(const string &word) const;  
  void write_out(const string &word) const;

private:
  vector<string> svec;
  map< int, pair<string, int> > result;
}

//The following line is line 21, where I got the error!!
vector<string> &TextQuery::read_file(ifstream &infile) const {
  while (getline(infile, line)) {
    svec.push_back(line);
  }
  return svec;
}

map< int, pair<string, int> > &TextQuery::find_word(const string &word) const {
  for (vector<string>::size_type i = 0; i != svec.end()-1; ++i) {
    int rep_per_line = 0;
    pos = svec[i].find(word, 0);
    while (pos != string::npos) {
      if (!result[i+1]) {
        result.insert(make_pair(i+1, make_pair(svec[i], rep_per_line)));
        ++result[i+1].second;
      }
      else {
        ++result[i+1].second;
      }
    }
  }
  return result;
}

void TextQuery::write_out(const string &word) {
  cout << " The word " << "'" << word << "'" << " repeats:" << endl;
  for (map< int, pair<string, int> >::const_iterator iter = result.begin(); iter != result.end(); ++iter) {
    cout << "(line " << (*iter).first << " - " << (*iter).second.second << " times): ";
    cout << result.second.first << endl; 
  }
}



А вот и остальная часть программы:

int main() 
{
  string word, ifile;
  TextQuery tq;  

  cout << "Type in the file name: " << endl;
  cin >> ifile;

  ifstream infile(ifile.c_str());

  tq.read_file(infile);

  cout << "Type in the word want to search: " << endl;
  cin >> word;

  tq.find_word(word);
  tq.write_out(word);

  return 0;
}


Спасибо за ответ на мой вопрос !!

6
задан 0x6900 4 November 2014 в 06:12
поделиться