我'm writing a program for my homework that counts words and lines. I'我想知道为什么我得到错误:"no instance of overloaded function " getline " matches the argument list. Argument types are (std::ifstream, int)"我确定"infile"是std :: ifstream参数类型 . 这个问题可能是视觉工作室还是我很快就会在没有事先知识的情况下责怪某些事情P.S我搜索了一下,但找不到一个完全相同的问题的线程..有类似的但他们最终是有人放一个文件名字符串而不是流本身 . Also keep in mind i'm in the middle of writing this i didn't finish yet.
#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();
}
1 回答
没有std::getline重载需要
int
秒参数 . 我假设您打算传递std::string
变量 .您应该删除
infile >> word;
行或决定是否要使用它或std::getline
. 在这种情况下,我认为你不想要两者 .这将修复编译器错误,但不会修复程序逻辑 . 如果您使用
std::getline
,则必须解析每一行以计算单词 .