c++ - **编译器错误** - getline() 函数不接受第一个参数 "std:ifstream"我的问题是什么?

标签 c++

我正在为我的家庭作业编写一个计算单词和行数的程序。我想知道为什么我会收到错误消息:“没有重载函数“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/

相关文章:

c++ - friend get 返回类型的函数,该函数通过可变参数模板递归计算

c++ - 使用声明为 `static const` 的 QRegExp 对象是否安全?

c++ - 将 char * 传递给 delphi dll 函数/过程

c++ - 隐式构造函数参数

c++ - Windows 库包含在 CMake 项目中

c++ - 在 LLVM 2.8 中调用 LLVM bitcode 函数

c++ - 静态成员效用函数与非成员效用函数

python - 如何将 Count Morgan 指纹计算为 numpy.array?

c++ - 在 C++ 中使用 AKS 素数测试计算双素数我做错了什么?

c++将给定.dat文件中的字符串 vector 转换为 double vector