[英]No operator “>>” for std::string>>std::string
I'm having a problem and I'm not sure how to fix it. 我遇到问题,不确定如何解决。
Here is a search function that I made from which I'm receiving the error; 这是我从中获得错误的搜索功能;
void guestSearch()
{
&Reservation::getfirstName;
&Reservation::getlastName;
&Date::getday;
&Date::getmonth;
&Date::getyear;
Hotelreservation reg;
reg.duration;
reg.occupants;
&Contact::getareaCode;
&Contact::getexchange;
&Contact::getline;
system("cls");
cout<<"Enter Line Number for the Guest you are searching for:";
cin>>line;
string line2= to_string(line);
line2.append(".txt");
ifstream filemain(line2);
while(firstName>>lastName>>day>>month>>year>>duration>>occupants>>areaCode>>exchange>>line)
{
int firstNameLength=firstName.size();
int lastNameLength=lastName.size();
int lengthTotal=firstName+lastName;
string result;
cout<< "Is this the correct Guest (y/n)"<<endl;
cout<<"Name"<<firstName<<" "<<lastName<<end;
cin>>result;
if(result=="y")
{
system("cls");
cout<<"Name";
for(int y = 1; y<lengthTotal; y++)
{
cout<< " ";
}
cout<<"Reservation Date";
for(int z=1; z<2; z++)
{
cout<< " ";
}
cout<<"Duration of Stay";
for(int x=1; x<2; x++)
{
cout<< " ";
}
cout<<"Occupants";
for(int u=1; u<2; u++)
{
cout<< " ";
}
cout<<"Contact Number";
for(int v=1; v<2; v++)
{
cout<< " ";
}
}
cout<<"Date Reservation was made:"<<day<<"/"<<month<<"/"<<year<<endl;
cout<<"Duration Of Stay:"<<duration<<endl;
cout<<"Occupants:"<<occupants<<endl;
cout<<"Contact Number:"<<areaCode<<"-"<<exchange<<"-"<<line<<endl;
}
}
I'm receiving an error at the while declaration after firstName ">>"
, I get this: 我在
firstName ">>"
之后的while声明中收到错误,得到的是:
IntelliSense: no operator ">>" matches these operands operand types are: std::string>>std::string"
IntelliSense:没有运算符“ >>”与这些操作数匹配,操作数类型为:std :: string >> std :: string“
You misunderstand the concept, though it is possible to chain multiple std::string
instances trhough operator >>
, first object should be of type inherited from std::istream
such as ifstream fileman
in your case: 您可能会误解这个概念,尽管可以通过
operator >>
链接多个std::string
实例,但第一个对象应该是从std::istream
继承的类型,例如ifstream fileman
:
while( fileman >> firstName >> lastName >> ... )
it works because you can treat that expression as: 之所以有效,是因为您可以将该表达式视为:
while( fileman.operator>>( firstName ).operator>>( lastName ) ... )
and because std::istream::operator>>
returns reference to std::istream
again that chain invocation works. 并且由于
std::istream::operator>>
再次返回对std::istream
引用,因此链调用有效。
There is no such operator in std::string
std::string
没有这样的运算符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.