我在構建unordeed_set<tuple<int,int>>
時遇到了奇怪的問題。我曾試過VC++ 8,gcc3.2,gcc4.3,都有相同的結果。我不知道什麼是錯的代碼,以下是我的代碼:如何unordered_set <tuple <int,int>>?
#include <boost/unordered_set.hpp>
#include <boost/tuple/tuple.hpp>
// For unordered container, the declaration of operator==
#include <boost/tuple/tuple_comparison.hpp>
using namespace std ;
using namespace boost ;
// define of the hash_value funciton for tuple<int, int>
size_t hash_value(tuple<int, int> const& t) {
return get<0>(t) * 10 + get<1>(t) ;
}
int main() {
unordered_set<tuple<int, int>> s ;
tuple<int, int> t ;
s.insert(t) ;
}
以下是編譯錯誤消息:
1>c:\libs\boost_1_37_0\boost\functional\hash\extensions.hpp(72) : error C2665: 'boost::hash_value' : none of the 16 overloads could convert all the argument types
1> c:\libs\boost_1_37_0\boost\functional\hash\hash.hpp(33): could be 'size_t boost::hash_value(bool)'
1> c:\libs\boost_1_37_0\boost\functional\hash\hash.hpp(34): or 'size_t boost::hash_value(char)'
1> c:\libs\boost_1_37_0\boost\functional\hash\hash.hpp(35): or 'size_t boost::hash_value(unsigned char)'
....
看來編譯器看不到的hash_value(tuple<int, int>)
定義。但是,如果我將tuple<int, int>
替換爲其他數據類型,如struct F{int a, b;}
並且它可以工作。這真的很奇怪。我錯過了什麼嗎?非常感謝你。
你實際使用'在代碼中使用namespace'?如果是這樣,那就拿出來。 – GManNickG 2009-08-09 04:20:21