我对于字符串类的>>运算符重载有问题;
这是我的课:

class str
{
    char s[250];
    public:
    friend istream& operator >> (istream& is, str& a);
    friend ostream& operator << (ostream& os, str& a);
    friend str operator + (str a, str b);
    str * operator = (str a);
    friend int operator == (str a, str b);
    friend int operator != (str a, str b);
    friend int operator > (str a, str b);
    friend int operator < (str a, str b);
    friend int operator >= (str a, str b);
    friend int operator <= (str a, str b);
};

这是重载运算符:
istream& operator >> (istream& in, str& a)
{
    in>>a.s;
    return in;
}

问题在于它仅将字符串读取到第一个空格(句子中只有一个单词)。

我解决了找到了关于dreamincode的答案:D

最佳答案

operator>> 的行为是读取直到第一个空格字符。将功能更改为以下内容:

istream& operator >> (istream& in, str& a)
{
    in.getline( a.s, sizeof(a.s) );
    return in;
}

07-24 09:46
查看更多