+ -
当前位置:首页 → 问答吧 → 我代码中C++的getline()怎么出错了?

我代码中C++的getline()怎么出错了?

时间:2011-12-05

来源:互联网

我的代码如下:


class Infor
{
private:
string name;
string call;
public:
Infor(string str1,string str2)
{
name = str1;
call = str2;
}
~Infor(void){}
void setName(string str1);
void setCall(string str2);
string getName(void)
{
return name;
}
string getCall(void)
{
return call;
}
bool operator == ( Infor &pt) const;
  bool operator < ( Infor &pt) const;
friend istream& operator>>(istream& infile,Infor& dt);
friend ostream& operator<<(ostream& outfile,Infor& dt);
};

void Infor::setName(string str1)
{
name = str1;
}
void Infor::setCall(string str2)
{
call = str2;
}
bool Infor::operator == ( Infor &pt) const
{
if(name == pt.getName())
return true;
else
return false;
}
bool Infor::operator < ( Infor &pt) const
{
  if(name < pt.getName())
return true;
else
return false;
}

istream& operator>>(istream& infile,Infor& dt)
{
string str1,str2;

infile.getline(str1,30,',');
infile.getline(str2,30,'\n');

dt.setName(str1);
dt.setCall(str2);
   
return infile;
}

ostream& operator<<(ostream& outfile,Infor& dt)
{
outfile<<dt.getName()<<','<<dt.getCall()<<endl;

return outfile;
}



编译不通过,说
istream& operator>>(istream& infile,Infor& dt)
{
string str1,str2;

infile.getline(str1,30,',');
infile.getline(str2,30,'\n');

dt.setName(str1);
dt.setCall(str2);
   
return infile;
}

这个错了

作者: xihojerry   发布时间: 2011-12-05

好吧。。。已经完结。
把string改成char*就可以了。。。

作者: xihojerry   发布时间: 2011-12-05

string str1,str2;----> char str1[30],str2[30];
试试

作者: Demon__Hunter   发布时间: 2011-12-05

这个比较完整的HELP
http://www.cplusplus.com/reference/iostream/istream/getline/

作者: keiy   发布时间: 2011-12-05