Chr*_*oph 1 c++ operator-overloading
可能重复:
如何在C++中重载一元减号运算符?
我有一个C类,它重载了许多运算符:
class C
{
...
C& operator-=(const C& other) {...}
const C operator-(const C& other) {...}
}
inline const C operator*(const double& lhs, const C& rhs)
Run Code Online (Sandbox Code Playgroud)
现在我想要反转一个C类型的对象
c = -c;
Run Code Online (Sandbox Code Playgroud)
并且gcc给了我以下错误:
no match for >>operator-<< in >>-d<<
candidate is: const C C::operator-(const C&)
Run Code Online (Sandbox Code Playgroud)
使用c = -1*c有效,但我希望能够缩短它.我班上缺少什么?
解决:添加了一元运算符 - :
C operator-() const {...}
Run Code Online (Sandbox Code Playgroud)
作为C的成员