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

**Compiler error** - getline() function not accepting first parameter "std:ifstream" what is my issue?

本文关键字:std ifstream 是什么 问题 我的 参数 第一个 错误 编译器 getline 函数      更新时间:2023-10-16

我正在为我的家庭作业写一个计算单词和行数的程序。我想知道为什么我得到错误:"没有重载函数"getline"的实例匹配参数列表。参数类型为(std::ifstream, int)"我确信"infile"是std::ifstream参数类型。问题可能是视觉工作室或我很快责怪没有事先的知识?附:我搜索了一点,但不会找到一个线程与完全相同的问题…有类似的,但他们最终是有人把文件名的字符串,而不是流本身。还要记住我正在写这篇文章,我还没有完成

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

没有接受int second参数的std::getline过载。我假设你的意思是传递你的std::string变量。

getline(infile, word);

您应该删除infile >> word;行或决定是否要使用它或std::getline。我认为在这种情况下你不需要两者。

这将修复编译器错误,但不会修复程序逻辑。如果使用std::getline,则必须解析每行以计算单词数。