samedi 16 juin 2018

Unordered_set with custom struct

I've been wanted to use unordered_set with a custom struct, in my case the custom struct represents a 2D point in a euclidian plane. Now, I know one should define a hash function and comparator operator, see below:

struct Point {
    int X;
    int Y;

    Point() : X(0), Y(0) {};
    Point(const int& x, const int& y) : X(x), Y(y) {};
    Point(const IPoint& other){
        X = other.X;
        Y = other.Y;
    };

    Point& operator=(const Point& other) {
        X = other.X;
        Y = other.Y;
        return *this;
    };

    bool operator==(const Point& other) {
        if (X == other.X && Y == other.Y)
            return true;
        return false;
    };

    bool operator<(const Point& other) {
        if (X < other.X )
            return true;
        else if (X == other.X && Y == other.Y)
            return true;

        return false;
    };

    size_t operator()(const Point& pointToHash) const {
        size_t hash = pointToHash.X + 10 * pointToHash.Y;
        return hash;
    };
};

Thing is I'm getting the following error even if I just define the set.

unordered_set<Point> mySet;

Error C2280 'std::hash<_Kty>::hash(const std::hash<_Kty> &)': attempting to reference a deleted function

Clearly missing something here but what???

Aucun commentaire:

Enregistrer un commentaire