unordermap 错误 invalid use of incomplete type
来源:互联网 发布:js radio 赋值选中 编辑:程序博客网 时间:2024/06/06 15:49
unordermap 错误 invalid use of incomplete type
今天在使用unordermap 保存自定义类型的时候出现了如下的错误:
In file included from /usr/include/c++/4.8/bits/hashtable.h:35:0, from /usr/include/c++/4.8/unordered_map:47, from ../include/engine.h:13, from superserver.h:1, from ServerTask.cpp:1:/usr/include/c++/4.8/bits/hashtable_policy.h: In instantiation of 'struct std::__detail::_Hash_code_base<Cmd::Super::ServerEntry, std::pair<const Cmd::Super::ServerEntry, bool>, std::__detail::_Select1st, std::hash<Cmd::Super::ServerEntry>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>':/usr/include/c++/4.8/bits/hashtable_policy.h:1402:10: required from 'struct std::__detail::_Hashtable_base<Cmd::Super::ServerEntry, std::pair<const Cmd::Super::ServerEntry, bool>, std::__detail::_Select1st, std::equal_to<Cmd::Super::ServerEntry>, std::hash<Cmd::Super::ServerEntry>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >'/usr/include/c++/4.8/bits/hashtable.h:174:11: required from 'class std::_Hashtable<Cmd::Super::ServerEntry, std::pair<const Cmd::Super::ServerEntry, bool>, std::allocator<std::pair<const Cmd::Super::ServerEntry, bool> >, std::__detail::_Select1st, std::equal_to<Cmd::Super::ServerEntry>, std::hash<Cmd::Super::ServerEntry>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >'/usr/include/c++/4.8/bits/unordered_map.h:100:18: required from 'class std::unordered_map<Cmd::Super::ServerEntry, bool>'superserver.h:221:12: required from here/usr/include/c++/4.8/bits/hashtable_policy.h:1070:12: error: invalid use of incomplete type 'struct std::hash<Cmd::Super::ServerEntry>' struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, ^In file included from /usr/include/c++/4.8/bits/basic_string.h:3033:0, from /usr/include/c++/4.8/string:52, from /usr/include/c++/4.8/bits/locale_classes.h:40, from /usr/include/c++/4.8/bits/ios_base.h:41, from /usr/include/c++/4.8/ios:42, from /usr/include/c++/4.8/istream:38, from /usr/include/c++/4.8/sstream:38, from ../include/engine.h:6, from superserver.h:1, from ServerTask.cpp:1:/usr/include/c++/4.8/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<Cmd::Super::ServerEntry>' struct hash; ^In file included from /usr/include/c++/4.8/bits/hashtable.h:35:0, from /usr/include/c++/4.8/unordered_map:47, from ../include/engine.h:13, from superserver.h:1, from ServerTask.cpp:1:/usr/include/c++/4.8/bits/hashtable_policy.h:1070:12: error: invalid use of incomplete type 'struct std::hash<Cmd::Super::ServerEntry>' struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, ^In file included from /usr/include/c++/4.8/bits/basic_string.h:3033:0, from /usr/include/c++/4.8/string:52, from /usr/include/c++/4.8/bits/locale_classes.h:40, from /usr/include/c++/4.8/bits/ios_base.h:41, from /usr/include/c++/4.8/ios:42, from /usr/include/c++/4.8/istream:38, from /usr/include/c++/4.8/sstream:38, from ../include/engine.h:6, from superserver.h:1, from ServerTask.cpp:1:/usr/include/c++/4.8/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<Cmd::Super::ServerEntry>' struct hash; ^In file included from /usr/include/c++/4.8/bits/hashtable.h:35:0, from /usr/include/c++/4.8/unordered_map:47, from ../include/engine.h:13, from superserver.h:1, from ServerTask.cpp:1:/usr/include/c++/4.8/bits/hashtable_policy.h:1082:53: error: invalid use of incomplete type 'struct std::hash<Cmd::Super::ServerEntry>' using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>; ^In file included from /usr/include/c++/4.8/bits/basic_string.h:3033:0, from /usr/include/c++/4.8/string:52, from /usr/include/c++/4.8/bits/locale_classes.h:40, from /usr/include/c++/4.8/bits/ios_base.h:41, from /usr/include/c++/4.8/ios:42, from /usr/include/c++/4.8/istream:38, from /usr/include/c++/4.8/sstream:38, from ../include/engine.h:6, from superserver.h:1, from ServerTask.cpp:1:/usr/include/c++/4.8/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<Cmd::Super::ServerEntry>' struct hash; ^In file included from /usr/include/c++/4.8/bits/hashtable.h:35:0, from /usr/include/c++/4.8/unordered_map:47, from ../include/engine.h:13, from superserver.h:1, from ServerTask.cpp:1:/usr/include/c++/4.8/bits/hashtable_policy.h:1082:53: error: invalid use of incomplete type 'struct std::hash<Cmd::Super::ServerEntry>' using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>; ^In file included from /usr/include/c++/4.8/bits/basic_string.h:3033:0, from /usr/include/c++/4.8/string:52, from /usr/include/c++/4.8/bits/locale_classes.h:40, from /usr/include/c++/4.8/bits/ios_base.h:41, from /usr/include/c++/4.8/ios:42, from /usr/include/c++/4.8/istream:38, from /usr/include/c++/4.8/sstream:38, from ../include/engine.h:6, from superserver.h:1, from ServerTask.cpp:1:/usr/include/c++/4.8/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<Cmd::Super::ServerEntry>' struct hash; ^ServerTask.cpp: In constructor 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with _Key = Cmd::Super::ServerEntry; _Tp = bool; _Hash = std::hash<Cmd::Super::ServerEntry>; _Pred = std::equal_to<Cmd::Super::ServerEntry>; _Alloc = std::allocator<std::pair<const Cmd::Super::ServerEntry, bool> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type = long unsigned int; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hasher = std::hash<Cmd::Super::ServerEntry>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_equal = std::equal_to<Cmd::Super::ServerEntry>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::allocator_type = std::allocator<std::pair<const Cmd::Super::ServerEntry, bool> >]':ServerTask.cpp:3:133: error: invalid use of incomplete type 'std::unordered_map<Cmd::Super::ServerEntry, bool>::hasher {aka struct std::hash<Cmd::Super::ServerEntry>}' ServerTask::ServerTask(zTCPTaskPool *tcptaskPool, const SOCKET sock, const struct sockaddr_in *addr) :zTCPTask(tcptaskPool,sock,addr) ^In file included from /usr/include/c++/4.8/bits/basic_string.h:3033:0, from /usr/include/c++/4.8/string:52, from /usr/include/c++/4.8/bits/locale_classes.h:40, from /usr/include/c++/4.8/bits/ios_base.h:41, from /usr/include/c++/4.8/ios:42, from /usr/include/c++/4.8/istream:38, from /usr/include/c++/4.8/sstream:38, from ../include/engine.h:6, from superserver.h:1, from ServerTask.cpp:1:/usr/include/c++/4.8/bits/functional_hash.h:58:12: error: declaration of 'std::unordered_map<Cmd::Super::ServerEntry, bool>::hasher {aka struct std::hash<Cmd::Super::ServerEntry>}' struct hash;
出现这个的原因是:没有给自定义类型设置hash函数,所以需要自己手动写一个。
class MyHash{public: std::size_t operator()(const vector<pair<int,int>> &v) const { std::size_t x = 0; for (auto &i : v) x ^= std::hash<int>()(i.first) ^ std::hash<int>()(i.second); return x; }};int main(){ unordered_set<vector<pair<int,int>>, MyHash> um;}
还有一种绕过语法错误的方法,就是对于自定义类型保存其指针,由于指针是int型,所以系统默认是支持的,不需要在写hash函数。但是要注意内存泄漏的问题。
0 0
- unordermap 错误 invalid use of incomplete type
- g++ 错误error: invalid use of incomplete type
- QT错误:error: invalid use of incomplete type 'struct QScrollBar'
- invalid use of incomplete type ‘struct QLineEdit’
- invalid use of incomplete type 报
- 关于提示:invalid use of incomplete type
- invalid use of incomplete type 'class QWidget'
- c++ class forward declaration and invalid use of incomplete type
- QT编译的问题- invalid use of incomplete type...
- invalid use of incomplete type ‘struct Ui::mydlg
- QT中invalid use of incomplete type 'struct QHostAddress'
- 解决:invalid application of `sizeof' to incomplete type `({anonymous})'错误
- QT invalid application of 'sizeof' to incomplete type 错误解决
- invalid use of incomplete type struct 或者是class的解决办法
- invalid use of incomplete type struct 或者是class的解决办法
- invalid use of incomplete type 'class-Ui-Widge't的解决办法
- invalid use of incomplete type struct 或者是class的解决办法
- Qt下编程出现 invalid use of incomplete type "class QThread"
- 欢迎使用CSDN-markdown编辑器
- 笔试题1
- Linux确认硬件驱动模块方法(modules.pcimap)
- ubuntu 添加桌面图标
- Flux --> Redux --> Redux React 入门 基础实例使用
- unordermap 错误 invalid use of incomplete type
- 周笔记(3/52) - DX11 - Lighting/Texturing/Blending/Stenciling
- PAT(甲级)1036. Boys vs Girls (25)
- 【NPM】常用包记录
- [ZJOI2009对称的正方形]ST+Manacher
- c#中ref的用法
- androidstudio 插件 butterknife 安装及应用
- 在路上
- More Effective C++学习笔记(4)-效率