我正在为我的家庭作业编写一个计算单词和行数的程序。我想知道为什么我会收到错误消息:“没有重载函数“getline”的实例与参数列表匹配。参数类型是 (std::ifstream, int)” 我确定“infile”是 std::ifstream 参数类型。问题可能出在 visual studio 上,还是我会在没有先验知识的情况下迅速归咎于某些事情? P.S 我搜索了一下,但没有找到具有完全相同问题的线程。有类似的但他们最终是有人输入了文件名字符串而不是流本身。 另请记住,我正在写这篇文章,但尚未完成。
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
ifstream infile;
infile.open("Lear.txt");
string word;
int countWords = 0, countLines = 0;
while (!infile.eof())
{
infile >> word;
countWords++;
getline(infile, countLines); //issue area here at infile
countLines++;
}
cout << "Words: " << setw(9) << countWords << endl;
cout << "Lines: " << setw(9) << countLines << endl;
infile.close();
}
最佳答案
没有 std::getline
采用 int
第二个参数的重载。我假设您打算传递您的 std::string
变量。
getline(infile, word);
您应该删除 infile >> word;
行或决定是使用它还是 std::getline
。在这种情况下,我认为您不需要两者。
这将修复编译器错误,但不会修复您的程序逻辑。如果您使用 std::getline
,则必须解析每一行以计算字数。
关于c++ - **编译器错误** - getline() 函数不接受第一个参数 "std:ifstream"我的问题是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32784888/