Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::无序映射插入错误_C++_Insert_C++11_Compiler Errors_Unordered Map - Fatal编程技术网

C++ std::无序映射插入错误

C++ std::无序映射插入错误,c++,insert,c++11,compiler-errors,unordered-map,C++,Insert,C++11,Compiler Errors,Unordered Map,我有下面的无序映射,它将指针映射到一个Item类型的对象和一个int类型的键 typedef std::unordered_map<int, Item*> ItemList; ItemList Items; 收益率: item_manager.cc: In member function ‘void ItemManager::addItem(Item*)’: item_manager.cc:31:51: error: no matching function for call to

我有下面的无序映射,它将指针映射到一个Item类型的对象和一个int类型的键

typedef std::unordered_map<int, Item*> ItemList;
ItemList Items;
收益率:

item_manager.cc: In member function ‘void ItemManager::addItem(Item*)’:
item_manager.cc:31:51: error: no matching function for call to ‘std::pair<const int, Item*>::pair(int&, Item&)’
item_manager.cc:31:51: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:140:2: note: template<class ... _Args1, class ... _Args2> std::pair::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>)
/usr/include/c++/4.6/bits/stl_pair.h:135:2: note: template<class _U1, class _U2> std::pair::pair(std::pair<_U1, _U2>&&)
/usr/include/c++/4.6/bits/stl_pair.h:131:2: note: template<class _U1, class _U2, class> std::pair::pair(_U1&&, _U2&&)
/usr/include/c++/4.6/bits/stl_pair.h:125:2: note: template<class _U2, class> std::pair::pair(const _T1&, _U2&&)
/usr/include/c++/4.6/bits/stl_pair.h:120:2: note: template<class _U1, class> std::pair::pair(_U1&&, const _T2&)
/usr/include/c++/4.6/bits/stl_pair.h:112:17: note: constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int, _T2 = Process*, std::pair<_T1, _T2> = std::pair<const int, Process*>]
/usr/include/c++/4.6/bits/stl_pair.h:112:17: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/4.6/bits/stl_pair.h:108:21: note: template<class _U1, class _U2> constexpr std::pair::pair(const std::pair<_U1, _U2>&)
/usr/include/c++/4.6/bits/stl_pair.h:103:26: note: constexpr std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const int, _T2 = Process*]
/usr/include/c++/4.6/bits/stl_pair.h:103:26: note:   no known conversion for argument 2 from ‘Process’ to ‘Process* const&’
/usr/include/c++/4.6/bits/stl_pair.h:99:26: note: constexpr std::pair<_T1, _T2>::pair() [with _T1 = const int, _T2 = Process*]
/usr/include/c++/4.6/bits/stl_pair.h:99:26: note:   candidate expects 0 arguments, 2 provided
item_manager.cc:在成员函数“void itemmager::addItem(item*)”中:
item_manager.cc:31:51:错误:调用“std::pair::pair(int&,item&)”时没有匹配函数
项目经理。抄送:31:51:注:候选人为:
/usr/include/c++/4.6/bits/stl\u pair.h:140:2:注:模板std::pair::pair(std::分段构造,std::tuple,std::tuple)
/usr/include/c++/4.6/bits/stl_pair.h:135:2:注:模板std::pair::pair(std::pair&)
/usr/include/c++/4.6/bits/stl_-pair.h:131:2:注意:模板std::pair::pair(_-U1&,_-U2&&)
/usr/include/c++/4.6/bits/stl_pair.h:125:2:注:模板std::pair::pair(const_T1&,_U2&&)
/usr/include/c++/4.6/bits/stl\u pair.h:120:2:注:模板std::pair::pair(_U1&,const\u T2&)
/usr/include/c++/4.6/bits/stl_pair.h:112:17:注:constepr std::pair::pair(const std::pair&)[带_T1=const int,_T2=Process*,std::pair=std::pair]
/usr/include/c++/4.6/bits/stl_pair.h:112:17:注意:候选者需要1个参数,提供2个
/usr/include/c++/4.6/bits/stl_pair.h:108:21:注意:模板constexpr std::pair::pair(const std::pair&)
/usr/include/c++/4.6/bits/stl_pair.h:103:26:注:constepr std::pair::pair(const_T1&,const_T2&)[带_T1=const int,_T2=Process*]
/usr/include/c++/4.6/bits/stl_pair.h:103:26:注意:参数2从“进程”到“进程*常量&”的转换未知
/usr/include/c++/4.6/bits/stl_pair.h:99:26:注意:constexpr std::pair::pair()[带_T1=const int,_T2=Process*]
/usr/include/c++/4.6/bits/stl_pair.h:99:26:注意:候选者需要0个参数,提供2个

你知道是什么导致了这些错误吗?对于C++,我是新手,所以我一直在研究我在Web上发现的无序映射的例子。非常感谢您的帮助。请,谢谢

不需要取消对指针的引用:

void ItemManager::addItem(Item *it)  {
    int i = it->getItemID();
    Items.insert(ItemList::value_type(i, it));
}

地图中的值属于
Item*
类型,因此需要插入
Item*
,而不是
Item
。这条线

 Items.insert(ItemList::value_type(i, *it));
应该是

Items.insert(ItemList::value_type(i, it));

@RMartin:最好的指针方法是不要使用它们,除非你必须这样做。我无法想象你不能在这里使用
std::unordered\u map
Items.insert(ItemList::value_type(i, it));