错误:‘&’ 之前的声明符无效代币

发布于 2024-10-29 11:01:12 字数 2801 浏览 6 评论 0原文

我试图编写一个 TextQuery 程序,允许用户:
1. 输入单词
2. 读取文件
3. 打印出单词出现在哪些行以及单词在该行出现的次数

我创建了一个名为 "TextQuery" 的类,其中包含 3 个成员函数:
1. "read_file" 读取文件并返回对向量的引用
2.“find_word”取需要搜索的单词
然后返回对地图的引用int,pair>
第一个“int”是行号,第二个“int”是单词在该行出现的次数,“字符串”< /strong> 是整行)
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;
}


谢谢你回答我的问题!!

I was trying to write a TextQuery program that allow user:
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" to write the result.

However, when I compiled the program I got this message:

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

I just wonder how can the declarator wrong? Here is the class definition section:

#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; 
  }
}

And here is the rest of the program:

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;
}

Thank you for answering my question!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感悟人生的甜 2024-11-05 11:01:12

类定义后缺少 ;

为什么会出现奇怪的错误消息?因为在该范围级别创建对象是完全合法的:

class ABC {
...
} globalABC;

Missing ; after the class definition.

Why the weird error message? Because it's entirely legal to create an object at that level of scope:

class ABC {
...
} globalABC;
囍孤女 2024-11-05 11:01:12

还有其他错误 - read_file 方法被声明为 const,因此您无法在其中调用非常量 vector::push_back (svec.push_back(line);)

There is other error - read_file method is declared as const, so you cannot call non-constant vector::push_back inside it (svec.push_back(line);)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文