简体   繁体   English

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

[英]**Compiler error** - getline() function not accepting first parameter “std:ifstream” what is my issue?

I'm writing a program for my homework that counts words and lines. 我正在为我的作业写一个程序,计算字数和行数。 I'm wondering why i get the error: "no instance of overloaded function "getline" matches the argument list. Argument types are (std::ifstream, int)" I was certain "infile" was of std::ifstream argument type. 我想知道为什么会收到错误:“没有重载函数“ getline”的实例与参数列表匹配。参数类型为(std :: ifstream,int)”我确定“ infile”属于std :: ifstream参数类型。 Could the problem be visual studio or am i quick to blame something without prior knowledge? 问题可能是视觉工作室,还是我会在没有先验知识的情况下迅速责备某些事情? PS i searched a bit but would not find a thread with exactly the same problem.. there are similar ones but they end up being that someone put a string of the file name and not the stream itself. PS我搜索了一点,但找不到完全相同的问题的线程..有类似的问题,但最终导致有人放置了文件名的字符串而不是流本身。 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();

}

There is no std::getline overload that takes an int second parameter. 没有std::getline重载采用int第二个参数。 I assume you meant to pass your std::string variable instead. 我假设您的意思是传递您的std::string变量。

getline(infile, word);

You should remove the infile >> word; 您应该删除infile >> word; line or decide whether you want to use it or std::getline . 行或决定是否要使用它或std::getline I don't think you want both in this case. 在这种情况下,我认为你们都不想要。

This will fix the compiler error but not your program logic. 这将解决编译器错误,但不能解决程序逻辑。 If you use std::getline you'll have to parse each line to count words. 如果使用std::getline ,则必须解析每一行以计算单词数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM