没有匹配函数来调用 getline()

no matching function for call to getline()

所以我正在做一些事情,但我似乎不明白为什么它不起作用。

void display_alls()
{
    ifstream fpd;
    fpd.open("student2.txt",ios::in);
    if(!fpd)
    {
        cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
        getch();
        return;
    }
    while(fpd)
    {   
        int pos;
        string seg;

        cout<<"\tUSN"<<setw(10)<<"\tName"<<setw(20)<<"Book Issued\n";
        //fp.seekg(pos,ios::beg);

        getline(fpd,st.usn,'|');
        getline(fpd,st.name,'|');
        getline(fpd,st.email,'|');
        getline(fpd,st.phone,'|');
        getline(fpd,st.stbno,'|');
        getline(fpd,seg);

        cout<<"\t"<<st.usn<<setw(20)<<st.name<<setw(10)<<st.stbno<<endl;
    }
    fp.close();
}

[Error] D:\library\library_v1.cpp:514: error: no matching function for call to `getline(std::ifstream&, char[20], char)'

错误出现在 getline 的每一行!但不在“getline(fpd,seg);

这个东西不能在 MingW 编译器上运行,但可以在我的大学系统上运行,idk 也许他们有旧的编译器,你能告诉我哪里出了问题吗? 非常感谢。

std::getline,如果这是您要使用的内容,则在 <string> header 中定义。你会想要包括它:

#include <string>

然后就可以通过std::getline调用了。如果你厌倦了输入 std::,你可以这样做:

using namespace std;

根据 IDE 或构建设置,这些事情有时已经可以为您完成。这可能就是为什么这适用于学校计算机,但不适用于您的计算机。

错误消息表明代码正在尝试读入一个包含 20 个字符的数组。不幸的是 std::getline 不处理字符数组。它只读入 std::string,这就是它与 string seg; 一起工作的原因。对于字符数组,您需要使用 std::istream::getlineLink to documentation page

然而,如果您可以用 std::strings 替换数据结构中的字符数组,您的生活可能会更轻松。