无匹配函数呼叫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 Issuedn";
        //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();
}

[错误] d: library library_v1.cpp:514:错误:无匹配函数 致电`getline(std :: ifstream&amp; char [20],char('

getline一起在每行上的错误!但不在" getline(fpd,seg);"

这件事在MINGW编译器上不起作用,但正在研究我的大学系统,IDK也许他们正在拥有较旧的编译器,您能告诉我什么问题吗?非常感谢。

错误消息表明该代码正在尝试读取20个字符的数组。不幸的是,std::getline不在字符阵列中处理。它仅读取到std::string,这就是为什么需要使用string seg;的字符数组来使用std::istream::getline的原因。链接到文档页面

,如果您可以用std::strings。

替换数据结构中的字符数组,那么生活可能会更轻松。

std::getline(如果是您要使用的(,则在<string>标题中定义。您将要包括它:

#include <string>

然后,您可以通过std::getline调用它。如果您厌倦了键入std::,则可以做:

using namespace std;

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

最新更新