Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to use "hash_map" on windows
    primarykey
    data
    text
    <p>I am trying to use the <strong>hash_map</strong> for my self-defined data structure <strong>Locations</strong>, which is in fact an array. When I tried to insert some value pairs, I got errors. But I don't know why.</p> <p>I am programming on Windows7 and I use VS 2010. Below is the minimum working set.</p> <pre><code> #include &lt;hash_map&gt; #include &lt;iostream&gt; 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 &amp; 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 &lt; HASH_NUM * 4; i ++) { hash = hash * seed + (*str++); } return (hash &amp; 0x7FFFFFFF); } }; //2. define less than function struct less_than{ bool operator()(const Locations &amp; x, const Locations &amp; y) const { for(int i = 0; i &lt; HASH_NUM; i ++){ if(x.loc[i] &lt; y.loc[i]) return true; } return false; } }; int main(){ hash_map&lt;Locations, unsigned int, hash_compare&lt;hash_Locations, less_than&gt; &gt; location_map; Locations x, y; for(int i = 0; i &lt; HASH_NUM; i ++){ x.loc[i] = i * 3; y.loc[i] = i * 2; } //error location_map.insert(pair&lt;Locations, unsigned int&gt;(x, 1)); location_map.insert(pair&lt;Locations, unsigned int&gt;(y, 2)); return 0; } </code></pre> <p>Edit: Here is the error report.</p> <pre><code> 1&gt;------ Build started: Project: hash_map, Configuration: Debug Win32 ------ 1&gt; hash_map.cpp 1&gt;d:\program files (x86)\microsoft visual studio 10.0\vc\include\xhash(854): error C2664: 'bool stdext::hash_compare&lt;_Kty,_Pr&gt;::operator ()(const _Kty &amp;,const _Kty &amp;) const' : cannot convert parameter 1 from 'const Locations' to 'const hash_Locations &amp;' 1&gt; with 1&gt; [ 1&gt; _Kty=hash_Locations, 1&gt; _Pr=less_than 1&gt; ] 1&gt; Reason: cannot convert from 'const Locations' to 'const hash_Locations' 1&gt; No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1&gt; d:\program files (x86)\microsoft visual studio 10.0\vc\include\xhash(849) : while compiling class template member function 'std::pair&lt;_Ty1,_Ty2&gt; std::_Hash&lt;_Traits&gt;::_Insert(const std::pair&lt;const _Kty,_Ty&gt; &amp;,std::_List_iterator&lt;_Mylist&gt;)' 1&gt; with 1&gt; [ 1&gt; _Ty1=std::_List_iterator&lt;std::_List_val&lt;std::pair&lt;const Locations,unsigned int&gt;,std::allocator&lt;std::pair&lt;const Locations,unsigned int&gt;&gt;&gt;&gt;, 1&gt; _Ty2=bool, 1&gt; _Traits=std::_Hmap_traits&lt;Locations,unsigned int,stdext::hash_compare&lt;hash_Locations,less_than&gt;,std::allocator&lt;std::pair&lt;const Locations,unsigned int&gt;&gt;,false&gt;, 1&gt; _Kty=Locations, 1&gt; _Ty=unsigned int, 1&gt; _Mylist=std::_List_val&lt;std::pair&lt;const Locations,unsigned int&gt;,std::allocator&lt;std::pair&lt;const Locations,unsigned int&gt;&gt;&gt; 1&gt; ] 1&gt; d:\program files (x86)\microsoft visual studio 10.0\vc\include\hash_map(91) : see reference to class template instantiation 'std::_Hash&lt;_Traits&gt;' being compiled 1&gt; with 1&gt; [ 1&gt; _Traits=std::_Hmap_traits&lt;Locations,unsigned int,stdext::hash_compare&lt;hash_Locations,less_than&gt;,std::allocator&lt;std::pair&lt;const Locations,unsigned int&gt;&gt;,false&gt; 1&gt; ] 1&gt; c:\documents and settings\daihuichen\桌面\hash_map\hash_map.cpp(42) : see reference to class template instantiation 'stdext::hash_map&lt;_Kty,_Ty,_Tr&gt;' being compiled 1&gt; with 1&gt; [ 1&gt; _Kty=Locations, 1&gt; _Ty=unsigned int, 1&gt; _Tr=stdext::hash_compare&lt;hash_Locations,less_than&gt; 1&gt; ] 1&gt;d:\program files (x86)\microsoft visual studio 10.0\vc\include\xhash(857): error C2664: 'bool stdext::hash_compare&lt;_Kty,_Pr&gt;::operator ()(const _Kty &amp;,const _Kty &amp;) const' : cannot convert parameter 1 from 'const Locations' to 'const hash_Locations &amp;' 1&gt; with 1&gt; [ 1&gt; _Kty=hash_Locations, 1&gt; _Pr=less_than 1&gt; ] 1&gt; Reason: cannot convert from 'const Locations' to 'const hash_Locations' 1&gt; 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 ========== </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload