1

在我的教科书中,他们给出了一个重载 + 运算符的例子

Sales_item operator+ (const Sales_item& lhs, const Sales_item& rhs)
 {
   Sales_item ret(lhs);
   ret += rhs;
   return ret;
 }

但是当我为我的链表尝试它时,它告诉我它只能接受零个或一个参数。

我没有看到什么,如果您不能接受 2 个参数,您将如何添加 2 个对象?

4

1 回答 1

1

If your operartor+ is a member function it doesn't need two arguments, as the object of the class you call operator+ on is the left-hand side argument. But you have already such an operator: operator+=. As it was suggested by chris and nims move operator+ outside Sales_item class.

于 2013-10-01T04:35:12.823 回答