c++ - std::string>>std::string 没有运算符 ">>"

标签 c++ operators stdstring operand

我遇到了问题,我不确定如何解决。

这是我创建的一个搜索功能,但我从中收到了错误;

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: no operator ">>" matches these operands operand types are: 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 中没有这样的操作符

关于c++ - std::string>>std::string 没有运算符 ">>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26911272/

相关文章:

php - 用PHP中的@运算符抑制错误

c++ - 如何将 C++ 字符串中的单词大写?

c++ - 多线程设计,C++保护全局成员

c++ - 在c++中用vector编译错误

javascript - 在 javascript 中使用 != null 和 != undefined 之间有什么功能区别吗?

c++ - 在 TypeScript 中翻译 c++ 函数

c++ - std::string::c_str 和 std::string::data 有什么区别?

c++ - 如何将 std::string 用于 char* 输出参数

c++ - 如何从进程内部确定 CPU 和内存消耗

c++ - 为什么迭代器类型与 vector 容器中的 value_type * 不匹配?