0

The following code that is a part of a chess game im making where the key are the position of the piece and the value is the possible moves that piece have

#include<iostream>
#include<map>
#include<vector>

using namespace std;

struct Coordinate{
    int x, y;
};

int main(){
    map<Coordinate, vector<Coordinate>> moves;//map that have an struct as key and a vector of structs as value.
    //There is the error
    moves.insert(make_pair(Coordinate{0,0},//the struct
                               vector<Coordinate>{Coordinate{1,1},//the vector
                                                  Coordinate{2,2},
                                                  Coordinate{3,3}}));
    return 0;
};

Thos code gets me to the line 235 in file 'stl_function.h'

1 Answers1

4

You need to provide a custom comparator to your struct:

struct Coordinate{
    int x, y;

    constexpr bool operator<(const Coordinate & rhs) const
    {
        return x < rhs.x && y < rhs.y;   
    }
};
hlscalon
  • 7,304
  • 4
  • 33
  • 40
  • Tanks that solved my problem. i am a starter with c++, can you tell me what is `rhs`? – Roberto Jaimes Mar 28 '16 at 21:03
  • 1
    @RobertoJaimes it stands for 'right hand side', cuz it's used for the right hand side of the `<` comparison. It's common to name that argument "rhs" or "other" – David Mar 28 '16 at 21:05