先上代码
-
#include "stdio.h"
-
#include <iostream>
-
#include <boost/unordered_set.hpp>
-
#include <boost/unordered_map.hpp>
-
#include <string>
-
#include <utility>
-
#include <boost/lexical_cast.hpp>
-
using namespace std;
-
using namespace boost;
-
-
class kindex
-
{
-
public:
-
kindex(uint32_t did,uint8_t utype):m_pd(did),m_ut(utype){;}
-
public:
-
uint32_t m_pd;
-
uint8_t m_ut;
-
//
-
friend bool operator==(kindex &,kindex &);
-
};
-
bool operator==(kindex &a,kindex &index)
-
{
-
return (a.m_pd == index.m_pd)&&(a.m_ut==index.m_ut);
-
}
-
size_t hash_value(const kindex& index)
-
{
-
return boost::hash<int>()((index.m_pd + index.m_ut*12967)%8933);
-
}
-
void test_unordered_map_find_diytype()
-
{
-
unordered_map<kindex,int> kmap;
-
kindex idx(11,12);
-
kmap[idx] = 13;
-
}
编译出现问题如下:
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也提供了,还缺什么呢?
最后看了:
-
template<typename _Tp>
-
struct equal_to : public binary_function<_Tp, _Tp, bool>
-
{
-
bool
-
operator()(const _Tp& __x, const _Tp& __y) const
-
{ return __x == __y; }
-
};
猜测可能是
bool operator==(kindex &a,kindex &index)
定义的形式不对,马上动手,改为:
bool operator==(const kindex &a,const kindex &index)
再编译,没问题了。 ^^
XX的,耽误了我一上午。
阅读(2038) | 评论(0) | 转发(0) |