Not very good in cpp I tried to create a map taking as key a pair of two values and as value an instance of another class. The key class is a Point, having a attribut X and Y.
The problem I found when using a map is that it used operator< to order the map and to find if two key are the same. But because I use a key with two values I cannot use it.
So I founded that unordered_map will prefer to use operator= for the verification, which I can use in my case.
But since then I couldn't create everything to make my class Point work as a key for my unordered_map.
I used as source :
My code :
I have put a clear version on a gist github : https://gist.github.com/DulcheE/826c3f00c585a2a5357ed806bc6871e5
I need to seperate everything between the .h and the .cpp. I can't only use a .cpp
Point.cpp :
bool operator==(const Point& a, const Point& b) {
return (a.X == b.X && a.Y == b.Y);
}
namespace std {
template<>
struct hash<Point>
{
std::size_t operator()(Point const& p) const
{
std::size_t h1 = std::hash<int>{}(p.getX());
std::size_t h2 = std::hash<int>{}(p.getY());
return h1 ^ (h2 << 1);
}
};
}
Definition of the map in my MapUser.h :
std::unordered_map<Point, Biome*>* map;
Initialization of the map in my MapUser.cpp :
this->map = new std::unordered_map<Point, Biome*>();
Logs :
...
Building 5 actions with 16 processes...
[1/5] Point.cpp
[2/5] Procedural_Terrain.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\INCLUDE\xhash(146): error C2064: le terme ne correspond pas ? une fonction qui prend 1 arguments
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\INCLUDE\xhash(172): note: voir les informations de r?f?rence sur le mod?le de variable 'const bool _Nothrow_hash<std::hash<Point>,Point>' en cours de compilation
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\INCLUDE\xhash(172): note: pendant la compilation de la fonction membre classe mod?le 'size_t std::_Uhash_compare<_Kty,_Hasher,_Keyeq>::operator ()<Point>(const _Keyty &) noexcept(<expr>) const'
with
[
_Kty=Point,
_Hasher=std::hash<Point>,
_Keyeq=std::equal_to<Point>,
_Keyty=Point
]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\INCLUDE\unordered_map(404): note: pendant la compilation de la fonction membre classe mod?le 'Biome *&std::unordered_map<Point,Biome *,std::hash<Point>,std::equal_to<Point>,std::allocator<std::pair<const Point,Biome *>>>::at(const Point &)'
E:\Documents\GitHub\...\Source\...\Private\MapUser.cpp(51): note: voir la r?f?rence ? l'instanciation de la fonction mod?le 'Biome *&std::unordered_map<Point,Biome *,std::hash<Point>,std::equal_to<Point>,std::allocator<std::pair<const Point,Biome *>>>::at(const Point &)' en cours de compilation
E:\Documents\GitHub\...\Source\...\Private\MapUser.cpp(13): note: voir la r?f?rence ? l'instanciation classe mod?le 'std::unordered_map<Point,Biome *,std::hash<Point>,std::equal_to<Point>,std::allocator<std::pair<const Point,Biome *>>>' en cours de compilation
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\INCLUDE\xhash(145): error C2056: expression non conforme
I also tried this :
template<>
struct equal_to<Point>
{
bool operator()(const Point& a, const Point& b)
{
return (a.getX() == b.getX() && a.getY() == b.getY());
}
};
this->map = new std::unordered_map<Point, Biome*, std::hash<Point>, std::equal_to<Point> >();
But I think in my case it just do the exact same thing.
Thanks for the ones that will take time to answer me, Have a nice day