简体   繁体   English

C++ 'getline' 没有重载的实例 function 匹配参数列表

[英]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.如果您希望必须从每一行读取这对值,则必须使用不同的策略。

  1. Read lines of text.阅读文本行。
  2. Process each line.处理每一行。
std::string line;
while ( fileOpening >> line )
{
   std::istringstream str(line);
   if (str >>  lengthInput >>   widthInput)
   {
      // Got the input. Use them.
   }
}

Important Note重要的提示

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.

相关问题 C ++没有重载函数getline的实例匹配参数列表 - C++ No instance of overloaded function getline matches argument list 错误没有重载函数的实例“getline”匹配参数列表c ++ - error no instance of overloaded function “getline” matches the argument list c++ getline在C ++中使用字符串返回错误:没有重载函数getline的实例与参数列表匹配 - getline using strings in C++ returning error: No instance of overloaded function getline matches the argument list 没有重载函数的实例“getline”匹配参数列表 - no instance of overloaded function “getline” matches argument list 错误没有重载的实例 function “getline” 与参数列表匹配 - Error No instance of overloaded function “getline” matches the argument list 没有重载函数的实例与参数列表匹配c ++ - no instance of overloaded function matches the argument list c++ 没有重载的实例 function 匹配参数列表 C++ - no instance of overloaded function matches argument list C++ c++ getline() 错误,没有重载函数“getline”的实例与参数列表匹配 - c++ getline() error, no instance of overload function "getline" matches the argument list 当使用带有int的getline时,如何修复“没有重载函数的实例”getline“匹配参数列表” - How to fix “no instance of overloaded function ”getline“ matches the argument list” when using getline with an int 没有重载的实例 function “getline”匹配参数列表——参数类型是:(std::ifstream,char) - no instance of overloaded function "getline" matches the argument list -- argument types are: (std::ifstream,char)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM