【问题标题】:No matching function when inserting into unordered_map插入 unordered_map 时没有匹配函数
【发布时间】:2012-12-23 05:24:03
【问题描述】:

我声明unordered_map 如下:

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

然后在其中插入一个元素(如果键不存在,则此映射将返回新元素的引用)

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

但我收到一条错误消息:

../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>&)’

你们能帮我解释一下这个错误吗?非常感谢!

【问题讨论】:

    标签: c++ boost hash unordered-map


    【解决方案1】:

    这是因为std::array&lt;char, 20&gt; 没有“默认”散列函数,至少没有实现提供。您必须为 std::array&lt;char, 20&gt; 提供散列函数,然后您的代码才能工作。

    正如您在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;
    

    您必须为 Key 类型提供 Hash 以提供您的自定义哈希函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      相关资源
      最近更新 更多