简体   繁体   English

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

[英]no matching function for call to getline()

so i am working on something and i cant seem to get why it isn't working. 所以我正在做某事,但我似乎无法理解为什么它不起作用。

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)' [错误] D:\\ library \\ library_v1.cpp:514:错误:没有匹配函数调用`getline(std :: ifstream&,char [20],char)'

error is on each line with getline ! getline每一行都有错误! but not in " getline(fpd,seg); " 但不在“ getline(fpd,seg); ”中

this thing isn't working on MingW compiler but was working on my college system, idk maybe they are having older compiler, can you please tell me what is wrong. 这个东西在MingW编译器上不起作用,但是在我的大学系统上工作,idk也许他们使用的是较旧的编译器,请您告诉我哪里出了问题。 much appreciated. 非常感激。

std::getline , if that's what you're trying to use, is defined in the <string> header. 如果您要使用的是std::getline ,则在<string>标头中定义。 You will want to include it: 您将要包括它:

#include <string>

Then you can call it via std::getline . 然后您可以通过std::getline调用它。 If you get tired of typing std:: , you can do: 如果您厌倦了键入std:: ,则可以执行以下操作:

using namespace std;

Depending on the IDE or build setup, these things can sometimes be done for you already. 根据IDE或构建设置的不同,有时这些事情可能已经为您完成。 It's possible that is why this works on the school computer, but not yours. 这可能就是为什么它可以在学校计算机上工作,而不是在您的计算机上工作的原因。

The error message suggests that the code is attempting to read into an array of 20 characters. 错误消息表明该代码正在尝试读入20个字符的数组。 Unfortunately std::getline does not deal in character arrays. 不幸的是std::getline不处理字符数组。 It only reads into std::string , which is why it works with string seg; 它只会读入std::string ,这就是为什么它可以与string seg;一起使用的原因string seg; For character arrays you need to use std::istream::getline . 对于字符数组,您需要使用std::istream::getline Link to documentation page 链接到文档页面

However life will probably be easier for you if you can replace the character arrays in the data structure with std::string s. 但是,如果可以用std::string替换数据结构中的字符数组,那么生活可能会更轻松。

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

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