繁体   English   中英

没有匹配的函数来调用GetLine()

[英]No Matching Function for call GetLine()

我试图在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声明之外创建字符串input解决此问题。 所以我做了:

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

对不起。

GetLine的参数是引用。 在C ++中,您不需要显式地为函数提供引用。 编译器将为您完成此操作。

因此,您的函数调用:

std::getline(&input,&line)

会变成:

std::getline(input, line)

您正在传递变量的地址 有点混乱对于一个初学者&指在可变型的上下文中参考而是指在使用可变的上下文地址。

对于该调用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字符串。 默认情况下,它被初始化为空字符串。

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

    • std::basic_istream<CharT,Traits>& input
    • std::basic_string<CharT,Traits,Allocator>& str
    • CharT delim
    • std::basic_istream<CharT,Traits>&& input
    • std::basic_string<CharT,Traits,Allocator>& str
    • CharT delim
    • std::basic_istream<CharT,Traits>& input
    • std::basic_string<CharT,Traits,Allocator>& str
    • std::basic_istream<CharT,Traits>& input
    • std::basic_string<CharT,Traits,Allocator>& str
    • CharT delim

您应该能够将参数从其引用地址( & )更改为实际值(no & )。 当函数要求&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;
}

输入此代码。 您可以暂时使它通用,它将暂时起作用。

//#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;
}  

暂无
暂无

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

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