マップまたはセットの2D座標を追跡しようとしています。
次のコードの結果、このエラーが発生します。エラーC2678:バイナリ '<':タイプ 'const _Ty'の左側のオペランドをとる演算子が見つかりません(または許容できる変換がありません)
struct Coord_T {
uint64_t x, y;
inline bool operator==(const Coord_T& o) { return x == o.x && y == o.y; }
inline bool operator<(const Coord_T& o) { return x < o.x || (x == o.x && y < o.y); }
inline bool operator>(const Coord_T& o) { return x > o.x || (x == o.x && y > o.y); }
inline bool operator!=(const Coord_T& o) { return x != o.x || y != o.y; }
inline bool operator<=(const Coord_T& o) { return x < o.x || (x == o.x && y <= o.y); }
inline bool operator>=(const Coord_T& o) { return x > o.x || (x == o.x && y >= o.y); }
};
int main()
{
Coord_T coord;
coord.x = 5;
coord.y = 6;
std::map<Coord_T, bool> vals;
vals[coord] = true;
return 0;
}
構造体に必要なすべての演算子を追加したと思うので、これを機能させるために他に何ができますか?
const関数としての演算子のオーバーロード:
#include <iostream>
#include <map>
struct Coord_T {
uint64_t x, y;
inline bool operator==(const Coord_T& o) const { return x == o.x && y == o.y; }
inline bool operator<(const Coord_T& o) const { return x < o.x || (x == o.x && y < o.y); }
inline bool operator>(const Coord_T& o) const { return x > o.x || (x == o.x && y > o.y); }
inline bool operator!=(const Coord_T& o) const { return x != o.x || y != o.y; }
inline bool operator<=(const Coord_T& o) const { return x < o.x || (x == o.x && y <= o.y); }
inline bool operator>=(const Coord_T& o) const { return x > o.x || (x == o.x && y >= o.y); }
};
int main()
{
Coord_T coord;
coord.x = 5;
coord.y = 6;
std::map<Coord_T, bool> vals;
vals[coord] = true;
return 0;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加