繁体   English   中英

<<操作符重载返回std :: string时出错

[英]Error with << operator overload returning a std::string

<<运算符重载的返回类型为std::string时,我很难理解编译器为何会出错。 你能帮我理解吗?

贝娄是一个可复制的示例,它给出了巨大的错误。

class XY
{
    int X__;
    int Y__;
 public:
    XY(int x, int y):X__(x), Y__(y){}
    ~XY(){}

    std::string operator<<(const XY_cartesiano& c)
    {
        std::stringstream ss;
        ss << "{ " << X__ << ", " << Y__ << " }";
        return ss.str();
    }

    int x() const{return X__;}
    int y() const{return Y__;}
};

void main()
{

XY a(1,2);
std::cout << a;
}

让我们以如下示例为例:

cout << "My number is " << 137 << " and I like it a lot." << endl;

这被解析为

((((cout << "My number is ") << 137) << " and I like it a lot.") << endl);

特别是,请注意表达式cout << "My number is "必须求值,以便当我们尝试在137中插入<< 137 ,含义是“接受137并将其发送给cout

想象一下,如果cout << "My number is "将返回一个string 在这种情况下, << 137位将尝试在左侧的string和右侧的int之间使用<<运算符,这在C ++中没有明确定义。

约定是让流插入操作operator <<返回对左侧流的引用,以使这些操作很好地链接。 这样, << 137左侧的东西最终将成为cout本身,因此上述代码实际上最终是一系列将它们插入cout的链式调用。 因此,这些功能的签名通常如下所示:

ostream& operator<< (ostream& out, const ObjectType& myObject) {
     // ... do something to insert myObject into out ... //
     return out;
}

现在,一切正常。 请注意,此函数是自由函数,而不是成员函数,并且左侧为ostream类型,右侧为您的类的类型。 这是执行此操作的常规方法,因为如果您尝试将operator <<作为成员函数重载,则左侧将是您的类类型的操作数,这与应该插入流的工作方式相反。 如果在实现此功能的过程中需要专门访问类的私有字段,请使其成为朋友:

class XY {
public:
      ...
      friend ostream& operator<< (ostream& out, const XY& myXY);
};

ostream& operator<< (ostream& out, const XY &myXY) {
    ...
    return out;
}

在您的情况下重载<<运算符的正确方法是

ostream& operator<<(ostream& os, const XY& c)  
{  
    os << c.X__ <<" "<< c.Y__ ;
    return os;  
}

您重载了operator<<的方式与打算将运算符与std::cout类的std::ostream对象一起使用时必须遵循的约定不兼容。

实际上,您的operator<<的签名与流完全无关! 它只是XY的成员函数,它使用另一个XY (然后不使用),返回一个字符串,并具有一个不正确的名称。 理论上,这是您的称呼方式:

XY a(1,2);
XY b(1,2);
std::string x = (a << b);

重载operator<<以与流一起使用的正确方法是使该运算符成为非成员函数,添加流引用参数并将流引用返回到流参数。 您也不需要字符串流。 您直接向流中写入内容:

#include <iostream>

class XY
{
    int x;
    int y;
public:
    XY(int x, int y) : x(x), y(y) {}

    int X() const { return x; }
    int Y() const { return y; }
};

std::ostream& operator<<(std::ostream& os, XY const& c)
{
    os << "{ " << c.X() << ", " << c.Y() << " }";
    return os;
}

int main()
{
    XY a(1,2);
    std::cout << a;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM