問題はそれです:
friend bool operator==(const TradeItem& item);
次の 2 つの引数が必要です。
bool TradeItem::operator==(const TradeItem& item)
1 つの引数が必要です。
これは、の非メンバー バージョンにはoperator==
常に 2 つ必要であり、メンバー バージョンには 1 つ必要であるためです。フレンド宣言で、非メンバー演算子のオーバーロードが宣言されています。
class C {
friend bool operator==(const C &lhs, const C &rhs);
public:
bool operator==(const C& rhs);
};
bool operator==(const C &lhs, const C &rhs) { return true; }
bool C::operator==(const C& rhs) { return true; }
また、左側で型変換を許可する必要がある場合にのみ、2 メンバー バージョンを使用する必要があります。例えば:
bool operator==(int lhs, const C &rhs);
bool operator==(double lhs, const C &rhs);
10 == C();
3.0 == C();
そして、これらのオーバーロードを C のフレンドとして宣言しなくても済む場合があります。次のようなものかもしれません。
class C {
public:
bool operator==(const C& rhs);
bool operator==(int rhs);
bool operator==(double rhs);
};
bool operator==(int lhs, const C &rhs) { return rhs == lhs; }
bool operator==(double lhs, const C &rhs) { return rhs == lhs; }
bool C::operator==(const C& rhs) { return true; }
bool C::operator==(int rhs) { return *this == convert_int_to_C(rhs); }
bool C::operator==(double rhs) { return *this == convert_double_to_C(rhs); }
また、等価性をチェックしてもオブジェクトは変更されないためconst
、メンバー関数を修飾する必要があります。
class C {
bool operator== (C const &rhs) const;
}