[英]C++ 'getline' No instance of overloaded function matches argument list
Trying to use getline but the error keeps saying: No instance of overloaded function matches argument list.尝试使用 getline 但错误一直说:没有重载 function 的实例与参数列表匹配。
#include <iomanip>
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int lengthInput;
int widthInput;
ifstream fileOpening;
fileOpening.open("testFile.txt");
while (!fileOpening.eof())
{
//ERROR ON NEXT TWO LINES OF CODE**
getline(fileOpening, lengthInput, ' ');
getline(fileOpening, widthInput, ' ');
}
system("pause");
return 0;
The 2nd parameter of std::getline()
expects a std::string
to write to, but in both cases you are passing in an int
instead. std::getline()
的第二个参数需要一个std::string
来写入,但在这两种情况下,您都在传递一个int
。 That is why you are getting the error - there really is no version of std::getline()
that matches your code.这就是您收到错误的原因 - 确实没有与您的代码匹配的
std::getline()
版本。
The second argument of getline
is expected to be a reference to a std::string
, not a reference to an int
. getline
的第二个参数应该是对std::string
的引用,而不是对int
的引用。
If you expect that the pair of values can be read from multiple lines, you can use:如果您希望这对值可以从多行中读取,您可以使用:
while (fileOpening >> lengthInput >> widthInput)
{
// Got the input. Use them.
}
If you expect that the pair of values must be read from each line, you'll have to use a different strategy.如果您希望必须从每一行读取这对值,则必须使用不同的策略。
std::string line;
while ( fileOpening >> line )
{
std::istringstream str(line);
if (str >> lengthInput >> widthInput)
{
// Got the input. Use them.
}
}
Don't use不要使用
while (!fileOpening.eof()) { ... }
See Why is iostream::eof inside a loop condition (ie `while (.stream?eof())`) considered wrong?请参阅为什么 iostream::eof 在循环条件内(即`while (.stream?eof())`)被认为是错误的? .
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.