【问题标题】:No Matching Function for call GetLine()调用 GetLine() 没有匹配函数
【发布时间】:2017-10-16 06:24:49
【问题描述】:

我正在尝试在 C++ 中创建一个函数,该函数从文件中读取所有行,然后将所有行连接成一个字符串,然后返回该字符串。这是我用来完成此操作的代码:

// at the top of the file

#include "string_helper.hpp" // contains definition for function
#include <vector>
#include <string.h> // used in another function within the same file
#include <fstream>
#include <sstream>
#include <iostream>

using namespace std;

// the code I believe to be problematic

ifstream input("file.txt");
string result(0);
string line;

while (std::getline(&input,&line)) {
    result += line;
}

但是,当我尝试编译此代码时,我收到此错误:

<project root>/src/string_helper.cpp:52:35: error: no matching function for call to ‘getline(std::ifstream (*)(std::__cxx11::string), std::__cxx11::string*)’
   while (std::getline(&input,&line)) {
                                   ^
In file included from /usr/include/c++/6.3.1/string:53:0,
                 from include/string_helper.hpp:22,
                 from <project root>/src/string_helper.cpp:19:

我查看了www.cplusplus.com,它列出了getline的定义:

istream& getline (istream& is, string& str);

我很困惑,因为我在 while 循环的声明中使用了 & 符号,但编译器仍然说我使用了不正确的参数。

编辑:原来我不小心创建了一个函数。 input 的真正声明是:

ifstream input(string(filename));

因为我必须从 char * 解析 filename。我没有将其包含在原始代码中,因为我试图使其通用,以便适用于许多人。我对 C++ 比较陌生。只需在 input 的声明之外创建字符串即可解决问题。所以我做了:

string fname(filename);
ifstream input(fname);

对不起。

【问题讨论】:

  • &amp; 用在不同的地方意味着不同的东西。一本好的 C++ 书籍会涵盖这一点。
  • 代码和错误消息不匹配 -- 除非您点击 most vexing parse,或者有一个名为 input 的函数。错误消息表明您传递的第一个参数是函数指针。

标签: c++ ifstream getline istream


【解决方案1】:

GetLine 的参数是引用。在 C++ 中,您不需要显式地为函数提供引用。编译器会为你做这些。

因此,您的函数调用:

std::getline(&input,&line)

会变成:

std::getline(input, line)

【讨论】:

  • 虽然这是正确的答案,但我希望 OP 回来说它不能解决问题,因为错误消息内容 getline(std::ifstream (*)(std::__cxx11::string), std::__cxx11::string*) 表明传递的第一个参数是函数指针,而不是指向 ifstream 的指针。 OP 的真实代码中可能隐藏着最令人头疼的解析(或简单的错字)。
  • 这不起作用。我得到了一个类似的错误,就像上面的错误一样,虽然类型末尾没有 *。
  • cdhowie: 真正的代码里面有以下内容: ifstream input(string(filename));因为它需要从指向字符的指针解析文件名。
【解决方案2】:

尝试确保您的 std::getline(...) 使用正确类型的变量作为其参数。

    • std::basic_istream&lt;CharT,Traits&gt;&amp; input
    • std::basic_string&lt;CharT,Traits,Allocator&gt;&amp; str
    • CharT delim
    • std::basic_istream&lt;CharT,Traits&gt;&amp;&amp; input
    • std::basic_string&lt;CharT,Traits,Allocator&gt;&amp; str
    • CharT delim
    • std::basic_istream&lt;CharT,Traits&gt;&amp; input
    • std::basic_string&lt;CharT,Traits,Allocator&gt;&amp; str
    • std::basic_istream&lt;CharT,Traits&gt;&amp; input
    • std::basic_string&lt;CharT,Traits,Allocator&gt;&amp; str
    • CharT delim

您应该能够将参数从它们的引用地址 (&amp;) 更改为它们的实际值(没有 &amp;)。当函数请求 &amp;value 时,通常可以直接将值传递给它。

http://en.cppreference.com/w/cpp/string/basic_string/getline

#include <string>
#include <fstream>

int main()
{
    std::ifstream input("C:\\temp\\file.txt");
    std::string result;
    std::string line;

    while (std::getline(input, line)) {
        result += line;
    }
    printf("Made it!\n");
    return 0;
}

【讨论】:

  • 尝试这样做会给我一些关于“CharT”、“Traits”和“Allactor”不存在的错误。
  • 谢谢,@CMan22!你能帮我一个忙吗?你能复制我更新的代码 sn-p 并重新构建它吗?我只是想确保正是 this 代码导致了错误。如果它仍然坏了,你能告诉我你使用的是哪个 C++ 吗?例如,GCC、Clang、Visual C++ 等。
  • 好的@CMan22,最后一个小更新。我必须更改result 的初始化程序,以使其与 Clang 和 GCC 兼容。这是我正在测试编译的站点:rextester.com/HXSUDT66780
【解决方案3】:

把这个代码。它会暂时起作用,您可以使其通用。

//#include "string_helper.hpp" // contains definition for function
#include <vector>
#include <string.h> // used in another function within the same file
#include <fstream>
#include <sstream>
#include <iostream>

using namespace std;
int main()
{
    ifstream input("file.txt");
    string result;
    char line[100];


    while (input.getline(line,'\n')) 
    {
        result += line;
    }
    cout<<"string = "<<result<<endl;
}  

【讨论】:

    【解决方案4】:

    您正在传递变量的地址。对于初学者来说有点混乱&amp; 表示在变量类型的上下文中引用,但在使用变量的上下文中表示地址。

    要使该调用起作用,getline 需要接受指针参数,但它不接受,因此根据提供的参数,没有匹配的函数调用。

    您无需执行任何特殊操作即可传递引用。传递给函数的引用和值之间的区别是由函数而不是调用者决定的。

    所以只需删除那些 & 符号就可以了。

    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        ifstream input("file.txt");
        string result;
        string line;
    
        while (getline(input, line)) 
        {
            result += line;
        }
    
        cout << result << '\n';
    }
    

    您也不需要初始化result 字符串。默认初始化为空字符串。

    【讨论】:

      猜你喜欢
      • 2018-10-09
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2020-10-21
      相关资源
      最近更新 更多