重複の可能性:
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)
今、タイプ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のメンバーとして