dimanche 19 juillet 2020

c++ struct as map key and operator overloading

I am trying to track 2d coordinates in a map or set.

The following code results in this error: error C2678: binary '<': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion)

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;
}

I believe I've added all the operators I need for the struct, so what else can I do to make this work?

Aucun commentaire:

Enregistrer un commentaire