所以我正在做一些事情,但我似乎不明白为什么它不起作用。
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 编译器上运行,但可以在我的大学系统上运行,我不知道他们可能使用的是较旧的编译器,你能告诉我出了什么问题吗? 非常感谢。
最佳答案
错误消息表明代码正在尝试读入 20 个字符的数组。不幸的是,std::getline 不处理字符数组。它只读取 std::string
,这就是它与 string seg;
一起使用的原因;对于字符数组,您需要使用 std::istream::getline
。 Link to documentation page
但是,如果您可以用 std::string
替换数据结构中的字符数组,生活可能会更轻松。
关于c++ - 没有匹配的函数来调用 getline(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50090926/