Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No matching function when inserting into unordered_map

I declare an unordered_map as following:

boost::unordered_map<std::array<char, 20>, t_torrent> torrent_ins;

and then insert an element into it (in the case the key hasn't existed, this map will return a reference for the new element)

t_torrent& torrent_in = torrent_ins[to_array<char,20>(in)];

But I got an error message:

../src/Tracker/torrent_serialization.cpp:30:   instantiated from here/usr/local/include/boost/functional/hash/extensions.hpp:176: error: no matching function    for call to ‘hash_value(const std::array<char, 20ul>&)’

Could you guys help me explain this error? Thanks so much!

like image 625
khanhhh89 Avatar asked Feb 18 '23 22:02

khanhhh89


1 Answers

It's because there's no "default" hashing function for std::array<char, 20>, at least none that the implementation furnishes. You must supply your hashing function for std::array<char, 20> then for your code to work.

As you can see in std::unordered_map,:

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

you must provide Hash for type Key to provide your custom hash function.

like image 106
Mark Garcia Avatar answered Feb 25 '23 12:02

Mark Garcia