簡體   English   中英

std :: string >> std :: string沒有運算符“ >>”

[英]No operator “>>” for std::string>>std::string

我遇到問題,不確定如何解決。

這是我從中獲得錯誤的搜索功能;

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

我在firstName ">>"之后的while聲明中收到錯誤,得到的是:

IntelliSense:沒有運算符“ >>”與這些操作數匹配,操作數類型為:std :: string >> std :: string“

您可能會誤解這個概念,盡管可以通過operator >>鏈接多個std::string實例,但第一個對象應該是從std::istream繼承的類型,例如ifstream fileman

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

之所以有效,是因為您可以將該表達式視為:

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

並且由於std::istream::operator>>再次返回對std::istream引用,因此鏈調用有效。

std::string沒有這樣的運算符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM