【发布时间】:2015-01-31 23:06:13
【问题描述】:
我无法对 std::tr1::unordered_map 使用插入函数,尝试构建时不断收到以下错误:
/usr/include/c++/4.2.1/tr1/hashtable:855:14: error: cannot initialize return object of type '_Node *' (aka '_Hash_node<std::pair<const unsigned long long, Order>, false> *') with an rvalue of type 'bool'
return false;
^~~~~
我的精简代码如下:
#include <tr1/unordered_map>
#include "handler.h"
#include "endian_tools.h"
using namespace std::tr1;
using namespace std;
unordered_map<uint64_t, Order> book_by_id;
uint64_t ref_num = be64toh(msg);
Order order(ref_num);
book_by_id.insert(make_pair<uint64_t,Order>(ref_num, order));
我认为这可能与我使用 long long 作为键的事实有关,但即使将其更改为 int 后,我也会遇到相同的错误。有什么想法吗?我在网上的任何地方都找不到其他出现此错误的人。
【问题讨论】:
-
你用的是什么编译器?
-
仅供参考,使用
std::make_pair函数的原因是您不必显式指定类型(让模板类型推导为您完成工作)。相反,您只需执行book_by_id.insert(make_pair(ref_num, order)); -
tr1::unordered_map?你的编译器几岁了? -
您的
tr1实现是否有专门的std::hash<uint64_t>?自从我查看某人的 tr1 impl 以来就一直存在,但可能值得你花时间看看你的。 -
我在 Eclipse 上使用 Mac OSX GCC 编译器。我使用 tr1 的原因是,如果我只执行
#include <unordered_map>,我在声明 unordered_map 时会收到invalid template arguments错误。我将其更改为 tr1 并解决了问题。我应该不使用 tr1 并尝试找到解决无效模板问题的另一种方法吗?
标签: c++ unordered-map tr1