【发布时间】:2012-11-17 21:25:36
【问题描述】:
我有一个重载许多运算符的 C 类:
class C
{
...
C& operator-=(const C& other) {...}
const C operator-(const C& other) {...}
}
inline const C operator*(const double& lhs, const C& rhs)
现在我想反转一个 C 类型的对象
c = -c;
而 gcc 给了我以下错误:
no match for >>operator-<< in >>-d<<
candidate is: const C C::operator-(const C&)
使用 c = -1*c 有效,但我希望能够缩短它。我的课缺少什么?
已解决:添加一元运算符-:
C operator-() const {...}
作为 C 的成员。
【问题讨论】:
-
我的情况略有不同 - 我不知道我必须重载一元运算符。