В unordered_map C2440 «приведение типа»: невозможно преобразовать … когда определены operator == и hash_value

При добавлении члена типа unordered_map в MyClass произошла ошибка компиляции C2440

Оператор == и hash_value () уже определены.

#include <unordered_map>
namespace MyNameSpace {
class MyClass {
public:
struct SomeArg { int x; int y; };
typedef void (MyClass::*FUNC)(MyClass*, MyClass::SomeArg);
struct SomeTuple { MyClass::FUNC a; int b; int c; };
void func(MyClass* myc, MyClass::SomeArg);
private:
// xfunctional(768): error C2440 'type cast' cannot convert 'SomeTuple' to 'size_t'
std::unordered_map<SomeTuple, int> someMap;
}; // end of MyClass
bool operator ==(const SomeTuple& a, const SomeTuple& b);
std::size_t hash_value(const MyClass::SomeTuple& t);
}
namespace std { // already tried moving here
//bool operator ==(const SomeTuple& a, const SomeTuple& b) {
//    return (a.a==b.a && a.b==b.b && a.c==b.c);
//}
//size_t hash_value(const MyNameSpace::MyClass::SomeTuple& t) {
//    size_t seed=0; boost::hash_combine(seed, t.x); boost::hash_combine(seed, t.y);
//}
}

Что мне не хватает?

0

Решение

Это не boost, Вы должны специализироваться std::hash для вашего типа, или дать предикат для map,

template <class Key,
class T,
class Hash = hash<Key>,
class Pred = std::equal_to<Key>,
class Alloc = std::allocator<std::pair<const Key, T> > >
class unordered_map;

Если твой SomeTuple не в классе, в котором unordered_map, где Key является SomeTuple, создается — это просто, а в другом случае — мне кажется невозможным.

#include <unordered_map>

namespace MyNameSpace {
struct SomeTuple { int a; int b; int c; };
}

namespace std {
template<>
struct hash<MyNameSpace::SomeTuple>
{
size_t operator ()();
};
}

namespace MyNameSpace {
class MyClass {
public:
struct SomeArg { int x; int y; };
void func(MyClass* myc, MyClass::SomeArg);
private:
// xfunctional(768): error C2440 'type cast' cannot convert 'SomeTuple' to 'size_t'
std::unordered_map<SomeTuple, int> someMap;
}; // end of MyClass
bool operator ==(const SomeTuple& a, const SomeTuple& b);
}
2

Другие решения

Я думаю, что вы имели в виду это
bool MyClass :: operator == (const SomeTuple& другое) const

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector