简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM