0

我有一个问题,我不知道如何解决它。

这是我收到错误的搜索功能;

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;
    }
}

我在 while 声明之后收到一个错误firstName ">>",我得到这个:

IntelliSense:没有操作符“>>”匹配这些操作数操作数类型是:std::string>>std::string”

4

1 回答 1

1

您误解了这个概念,尽管可以通过链接多个std::string实例operator >>,但第一个对象应该是从您的情况继承的std::istream类型ifstream fileman

while( fileman >> firstName >> lastName >> ... )

它之所以有效,是因为您可以将该表达式视为:

while( fileman.operator>>( firstName ).operator>>( lastName ) ... )

并且因为再次std::istream::operator>>返回std::istream对该链调用的引用。

中没有这样的运算符std::string

于 2014-11-13T14:51:40.707 回答