title: error: invalid declarator before ‘&’ token
body: <p>I was trying to write a TextQuery program that allow user: <br>
  1. <strong>inputs a word</strong> <br>
  2. <strong>reads a file</strong> <br>
  3. <strong>prints out which lines the words appear</strong> and <strong>how many times the word appears on that line</strong>. <br> <br></p>
  
  <p>I created a class called <strong>"TextQuery"</strong> with 3 member functions: <br>
  1. <strong>"read_file"</strong> to read the file and return a reference to a vector <br>
  2. <strong>"find_word"</strong> to take the word needs to be searched <br>
     then returns a reference to a <strong>map&lt; int, pair ></strong> <br>
     (<strong>the 1st 'int'</strong> is the line number, <strong>the 2nd 'int'</strong> is the number of times the word occurs on that line, <strong>the 'string'</strong> is the whole line) <br>
  3. "write_out" to write the result. <br> <br></p>
  
  <p>However, when I compiled the program I got this message:</p>
  
  <pre><code>/home/phongcao/C++/textquery_class_1.cc:21: error: invalid declarator before ‘&amp;’ token
  </code></pre>
  
  <p><br> </p>
  
  <p>I just wonder how can the declarator wrong? Here is the class definition section:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;algorithm&gt;
  #include &lt;map&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  class TextQuery {
  public:
    vector&lt;string&gt; &amp;read_file(ifstream &amp;infile) const;
    map&lt; int, pair&lt;string, int&gt; &gt; &amp;find_word(const string &amp;word) const;  
    void write_out(const string &amp;word) const;
  
  private:
    vector&lt;string&gt; svec;
    map&lt; int, pair&lt;string, int&gt; &gt; result;
  }
  
  //The following line is line 21, where I got the error!!
  vector&lt;string&gt; &amp;TextQuery::read_file(ifstream &amp;infile) const {
    while (getline(infile, line)) {
      svec.push_back(line);
    }
    return svec;
  }
  
  map&lt; int, pair&lt;string, int&gt; &gt; &amp;TextQuery::find_word(const string &amp;word) const {
    for (vector&lt;string&gt;::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 &amp;word) {
    cout &lt;&lt; " The word " &lt;&lt; "'" &lt;&lt; word &lt;&lt; "'" &lt;&lt; " repeats:" &lt;&lt; endl;
    for (map&lt; int, pair&lt;string, int&gt; &gt;::const_iterator iter = result.begin(); iter != result.end(); ++iter) {
      cout &lt;&lt; "(line " &lt;&lt; (*iter).first &lt;&lt; " - " &lt;&lt; (*iter).second.second &lt;&lt; " times): ";
      cout &lt;&lt; result.second.first &lt;&lt; endl; 
    }
  }
  </code></pre>
  
  <p><br> <br>
  And here is the rest of the program:</p>
  
  <pre><code>int main() 
  {
    string word, ifile;
    TextQuery tq;  
  
    cout &lt;&lt; "Type in the file name: " &lt;&lt; endl;
    cin &gt;&gt; ifile;
  
    ifstream infile(ifile.c_str());
  
    tq.read_file(infile);
  
    cout &lt;&lt; "Type in the word want to search: " &lt;&lt; endl;
    cin &gt;&gt; word;
  
    tq.find_word(word);
    tq.write_out(word);
  
    return 0;
  }
  </code></pre>
  
  <p><br> 
  Thank you for answering my question!! </p>