2013-03-21 49 views
0

我想爲我自定義的數據結構使用hash_map位置,它實際上是一個數組。當我嘗試插入一些值對時,出現錯誤。但我不知道爲什麼。如何在窗口上使用「hash_map」

我在Windows7上編程,並使用VS 2010.下面是最小工作集。

#include <hash_map> 
#include <iostream> 
using namespace std; 

#define HASH_NUM 8 

struct Locations{ 
    unsigned int loc[HASH_NUM]; 
}; 

//1. define the hash function 
struct hash_Locations{ 

    size_t operator()(const Locations & Loc) const 
    { 
     unsigned int seed = 131; // 31 131 1313 13131 131313 etc.. 
     unsigned int hash = 0; 
     char* str = (char*)Loc.loc; 

     for (int i = 0; i < HASH_NUM * 4; i ++) 
     { 
      hash = hash * seed + (*str++); 
     } 

     return (hash & 0x7FFFFFFF); 
    } 
}; 

//2. define less than function 
struct less_than{ 
    bool operator()(const Locations & x, const Locations & y) const 
    { 
     for(int i = 0; i < HASH_NUM; i ++){ 
      if(x.loc[i] < y.loc[i]) 
       return true; 
     } 
     return false; 
    } 
}; 

int main(){ 
    hash_map<Locations, unsigned int, hash_compare<hash_Locations, less_than> > location_map; 
    Locations x, y; 
    for(int i = 0; i < HASH_NUM; i ++){ 
     x.loc[i] = i * 3; 
     y.loc[i] = i * 2; 
    } 

    //error 
    location_map.insert(pair<Locations, unsigned int>(x, 1)); 
    location_map.insert(pair<Locations, unsigned int>(y, 2)); 


    return 0; 
} 

編輯: 這是錯誤報告。

1>------ Build started: Project: hash_map, Configuration: Debug Win32 ------ 
    1> hash_map.cpp 
    1>d:\program files (x86)\microsoft visual studio 10.0\vc\include\xhash(854): error C2664: 'bool stdext::hash_compare<_Kty,_Pr>::operator()(const _Kty &,const _Kty &) const' : cannot convert parameter 1 from 'const Locations' to 'const hash_Locations &' 
    1>   with 
    1>   [ 
    1>    _Kty=hash_Locations, 
    1>    _Pr=less_than 
    1>   ] 
    1>   Reason: cannot convert from 'const Locations' to 'const hash_Locations' 
    1>   No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 
    1>   d:\program files (x86)\microsoft visual studio 10.0\vc\include\xhash(849) : while compiling class template member function 'std::pair<_Ty1,_Ty2> std::_Hash<_Traits>::_Insert(const std::pair<const _Kty,_Ty> &,std::_List_iterator<_Mylist>)' 
    1>   with 
    1>   [ 
    1>    _Ty1=std::_List_iterator<std::_List_val<std::pair<const Locations,unsigned int>,std::allocator<std::pair<const Locations,unsigned int>>>>, 
    1>    _Ty2=bool, 
    1>    _Traits=std::_Hmap_traits<Locations,unsigned int,stdext::hash_compare<hash_Locations,less_than>,std::allocator<std::pair<const Locations,unsigned int>>,false>, 
    1>    _Kty=Locations, 
    1>    _Ty=unsigned int, 
    1>    _Mylist=std::_List_val<std::pair<const Locations,unsigned int>,std::allocator<std::pair<const Locations,unsigned int>>> 
    1>   ] 
    1>   d:\program files (x86)\microsoft visual studio 10.0\vc\include\hash_map(91) : see reference to class template instantiation 'std::_Hash<_Traits>' being compiled 
    1>   with 
    1>   [ 
    1>    _Traits=std::_Hmap_traits<Locations,unsigned int,stdext::hash_compare<hash_Locations,less_than>,std::allocator<std::pair<const Locations,unsigned int>>,false> 
    1>   ] 
    1>   c:\documents and settings\daihuichen\桌面\hash_map\hash_map.cpp(42) : see reference to class template instantiation 'stdext::hash_map<_Kty,_Ty,_Tr>' being compiled 
    1>   with 
    1>   [ 
    1>    _Kty=Locations, 
    1>    _Ty=unsigned int, 
    1>    _Tr=stdext::hash_compare<hash_Locations,less_than> 
    1>   ] 
    1>d:\program files (x86)\microsoft visual studio 10.0\vc\include\xhash(857): error C2664: 'bool stdext::hash_compare<_Kty,_Pr>::operator()(const _Kty &,const _Kty &) const' : cannot convert parameter 1 from 'const Locations' to 'const hash_Locations &' 
    1>   with 
    1>   [ 
    1>    _Kty=hash_Locations, 
    1>    _Pr=less_than 
    1>   ] 
    1>   Reason: cannot convert from 'const Locations' to 'const hash_Locations' 
    1>   No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+0

-1說不清楚/無用的「我得到了錯誤」並且沒有指定*什麼*錯誤。 – 2013-03-21 14:13:10

+0

@lc:添加錯誤報告。 – Bloodmoon 2013-03-21 17:05:39

回答

0

以下問題可以解決您的問題,但不能確定最佳解決方案。

struct hash_Locations{ 
// struct hash_Locations:public hash_compare<Locations, less_than>{ 
enum 
{ // parameters for hash table 
    bucket_size = 4, // 0 < bucket_size 
    min_buckets = 8}; // min_buckets = 2 ^^ N, 0 < N 
size_t operator()(const Locations & Loc) const 
{ 
    unsigned int seed = 131; // 31 131 1313 13131 131313 etc.. 
    unsigned int hash = 0; 
    char* str = (char*)Loc.loc; 

    for (int i = 0; i < HASH_NUM * 4; i ++) 
    { 
     hash = hash * seed + (*str++); 
    } 

    return (hash & 0x7FFFFFFF); 
} 

bool operator()(const Locations & x, const Locations & y) const 
{ 
    for(int i = 0; i < HASH_NUM; i ++){ 
     if(x.loc[i] < y.loc[i]) 
      return true; 
    } 
    return false; 
} 

};

0

我認爲錯誤報告說得很清楚。

「無法從 'const的位置' 到 '常量 hash_Locations &' 轉換參數1」

只要改變

bool operator()(const Locations & x, const Locations & y) 

bool operator()(const Locations x, const Locations y) 
相關問題