実際には配列である自己定義のデータ構造Locationsに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 ==========