Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1237107
  • 博文数量: 389
  • 博客积分: 2874
  • 博客等级: 少校
  • 技术积分: 3577
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:34
文章分类

全部博文(389)

文章存档

2020年(2)

2018年(39)

2017年(27)

2016年(3)

2015年(55)

2014年(92)

2013年(54)

2012年(53)

2011年(64)

分类: C/C++

2014-02-15 15:18:29

先上代码

点击(此处)折叠或打开

  1. #include "stdio.h"
  2. #include <iostream>
  3. #include <boost/unordered_set.hpp>
  4. #include <boost/unordered_map.hpp>
  5. #include <string>
  6. #include <utility>
  7. #include <boost/lexical_cast.hpp>
  8. using namespace std;
  9. using namespace boost;

  10. class kindex
  11. {
  12. public:
  13.     kindex(uint32_t did,uint8_t utype):m_pd(did),m_ut(utype){;}
  14. public:
  15.     uint32_t m_pd;
  16.     uint8_t m_ut;
  17.     //
  18.     friend bool operator==(kindex &,kindex &);
  19. };
  20. bool operator==(kindex &a,kindex &index)
  21. {
  22.     return (a.m_pd == index.m_pd)&&(a.m_ut==index.m_ut);
  23. }
  24. size_t hash_value(const kindex& index)
  25. {
  26.     return boost::hash<int>()((index.m_pd + index.m_ut*12967)%8933);
  27. }

点击(此处)折叠或打开

  1. void test_unordered_map_find_diytype()
  2.     {
  3.         unordered_map<kindex,int> kmap;
  4.         kindex idx(11,12);
  5.         kmap[idx] = 13;
  6.     }
编译出现问题如下:
d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_function.h||In member function 'bool std::equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = kindex]':|
d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\unordered\detail\table.hpp|29|instantiated from 'bool boost::unordered_detail::hash_table::equal(const typename T::key_type&, const typename T::value_type&) const [with T = boost::unordered_detail::map, std::equal_to, std::allocator > >]'|
d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\unordered\detail\table.hpp|56|instantiated from 'typename T::node_ptr boost::unordered_detail::hash_table::find_iterator(typename T::bucket_ptr, const typename T::key_type&) const [with T = boost::unordered_detail::map, std::equal_to, std::allocator > >]'|
d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\unordered\detail\unique.hpp|194|instantiated from 'typename boost::unordered_detail::hash_unique_table::value_type& boost::unordered_detail::hash_unique_table::operator[](const typename T::key_type&) [with T = boost::unordered_detail::map, std::equal_to, std::allocator > >]'|
d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\unordered\unordered_map.hpp|417|instantiated from 'T& boost::unordered_map::operator[](const K&) [with K = kindex, T = int, H = boost::hash, P = std::equal_to, A = std::allocator >]'|
F:\ns\code\QuantumKeyExport\unittest\main.cpp|115|instantiated from here|
d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_function.h|203|error: no match for 'operator==' in '__x == __y'|
F:\ns\code\QuantumKeyExport\unittest\main.cpp|26|note: candidates are: bool operator==(kindex&, kindex&)|
||=== Build finished: 1 errors, 0 warnings ===|

问题是    kmap[idx] = 13 这句,XX的,看了一遍又一遍 unordered_map,可是就是没有找到原因,“==”重载了,hash_value也提供了,还缺什么呢?
最后看了:

点击(此处)折叠或打开

  1. template<typename _Tp>
  2.     struct equal_to : public binary_function<_Tp, _Tp, bool>
  3.     {
  4.       bool
  5.       operator()(const _Tp& __x, const _Tp& __y) const
  6.       { return __x == __y; }
  7.     };
猜测可能是
bool operator==(kindex &a,kindex &index)
定义的形式不对,马上动手,改为:
bool operator==(const kindex &a,const kindex &index)
再编译,没问题了。 ^^
XX的,耽误了我一上午。
阅读(1965) | 评论(0) | 转发(0) |
0

上一篇:穿山甲的母爱

下一篇:Kvm管理工具之virsh

给主人留下些什么吧!~~