3

I am having a class which is used as the key in the unordered_map. When I tried to compiled the code, it shows undefined reference to std::hash<typeName>::operator()(typename) const. How could I go to fix it? What additional function do I need to overload to make the user defined type to be used in an unordered_map?

I have a dateTime struct which stores the info of date and time.

The error message is as follows:

In function 'std::__detail::_Hash_code_base<DateTime, std::pair<DateTime const, int>, std::_Select1st<std::pair<DateTime const, int> >, std::equal_to<DateTime>, std::hash<DateTime>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>::_M_hash_code(DateTime const&) const': testing.cpp:(.text._ZNKSt8__detail15_Hash_code_baseI10DateTimeSt4pairIKS1_DeESt10_Select1stIS4_ESt8equal_toIS1_ESt4hashIS1_ENS_18_Mod_range_hashingENS_20_Default_ranged_hashELb0EE12_M_hash_codeERS3_[std::__detail::_Hash_code_base<DateTime, std::pair<DateTime const, int>, std::_Select1st<std::pair<DateTime const, int> >, std::equal_to<DateTime>, std::hash<DateTime>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>::_M_hash_code(DateTime const&) const]+0x23): undefined reference to 'std::hash<DateTime>::operator()(DateTime) const'

Thanks.

Steven
  • 31
  • 1
  • 4
  • 1
    Your question is sorely lacking in code, the exact and _full_ error message, and information on what compiler you're using. – Nicholas Knight Nov 18 '10 at 03:26
  • 1
    I think this is a duplicate of http://stackoverflow.com/questions/647967/how-to-extend-stdtr1hash-for-custom-types – SCFrench Nov 18 '10 at 03:41
  • If anyone knows how to escape a backtick inside backticks, feel free to to so (I changed them to ' in my edit). The lines are too long for "indent by 4 spaces" to be any use. By the error message, compiler is some variety of GCC. – Steve Jessop Nov 18 '10 at 03:48

1 Answers1

4

you have to implement hash algorithm, otherwise standard container will not pick your type, because it has no idea how to calculate hash code for it.

namespace std
{    
   template <>
   struct hash<DateTime> : public unary_function<DateTime, size_t>
   {
       size_t operator()(const DateTime& v) const
       {
           return /* my hash algorithm */;
       }
   };
}
Andrey
  • 59,039
  • 12
  • 119
  • 163
  • Thanks for the reply. My datetime struct has the ticks value which I think could be used as the hash key. Is there method that could let me set the unordered_map to use the ticks value as hash key directly? – Steven Nov 18 '10 at 03:54
  • @Steven: If it's a public data member you could get the hash functor you need with `boost::mem_fun(&DateTime::ticks)`, but I dread to think what the type is that you'd have to set as the second template argument for `std::unordered_map`. Probably easier just to write the specialization. – Steve Jessop Nov 18 '10 at 04:06
  • @Steven: in Andrey's code above, have you tried "v.ticks" where Andrey said to put `/* [your] hash algorithm */`...? – Tony Delroy Nov 18 '10 at 04:09
  • @Steven yeah, try what Tony says – Andrey Nov 18 '10 at 16:56