views:

490

answers:

1

Hi,

I'm using the unordered_map of TR1 implementation in my code and the linker gives weird errors I cannot even decipher:

BPCFG.o: In function `std::__detail::_Hash_code_base<DottedRule, std::pair<DottedRule  const, int>, std::_Select1st<std::pair<DottedRule const, int> >, eqDottedRule,  std::hash<DottedRule>, std::__detail::_Mod_range_hashing,  std::__detail::_Default_ranged_hash, false>::_M_hash_code(DottedRule const&) const':  
BPCFG.cpp:  (.text._ZNKSt8__detail15_Hash_code_baseI10DottedRuleSt4pairIKS1_iESt10_Select1stIS4_E12eqDottedRuleSt4hashIS1_ENS_18_Mod_range_hashingENS_20_Default_ranged_hashELb0EE12_M_hash_codeERS3_[std::__detail::_Hash_code_base<DottedRule,   std::pair<DottedRule const, int>, std::_Select1st<std::pair<DottedRule const, int> >,   eqDottedRule, std::hash<DottedRule>, std::__detail::_Mod_range_hashing,  std::__detail::_Default_ranged_hash, false>::_M_hash_code(DottedRule const&) const]+0x23):  undefined reference to `std::hash<DottedRule>::operator()(DottedRule) const'
BPCFG.o: In function `std::__detail::_Hash_code_base<DottedRule, std::pair<DottedRule  const, int>, std::_Select1st<std::pair<DottedRule const, int> >, eqDottedRule,  std::hash<DottedRule>, std::__detail::_Mod_range_hashing,  std::__detail::_Default_ranged_hash,  false>::_M_bucket_index(std::__detail::_Hash_node<std::pair<DottedRule const, int>, false>  const*, unsigned long) const':
BPCFG.cpp:  (.text._ZNKSt8__detail15_Hash_code_baseI10DottedRuleSt4pairIKS1_iESt10_Select1stIS4_E12eqDottedRuleSt4hashIS1_ENS_18_Mod_range_hashingENS_20_Default_ranged_hashELb0EE15_M_bucket_indexEPKNS_10_Hash_nodeIS4_Lb0EEEm[std::__detail::_Hash_code_base<DottedRule,  std::pair<DottedRule const, int>, std::_Select1st<std::pair<DottedRule const, int> >,  eqDottedRule, std::hash<DottedRule>, std::__detail::_Mod_range_hashing,  std::__detail::_Default_ranged_hash,  false>::_M_bucket_index(std::__detail::_Hash_node<std::pair<DottedRule const, int>, false>  const*, unsigned long) const]+0x33): undefined reference to `std::hash<DottedRule>::operator()(DottedRule) const'  
collect2: ld returned 1 exit status

This is the error and I cannot even detect the line it's oriented from? From the statement:

undefined reference to `std::hash::operator()(DottedRule) const'

I guess that it's about the usage of hash. Now, the whole code is too big (if you nevertheless want to see it I may post it later), but the relevant parts are:

# include <unordered_map>       // Used as hash table
# include <stdlib.h>
# include <string.h>
# include <vector>

# define NO_SYMBOL -1

using namespace std;
using std::unordered_map;
using std::hash;

...
...
...

class DottedRule {
     public: 
         int symbol; 
         int expansion; 
         int dot_position;
 };

struct eqDottedRule
{
  bool operator()(const DottedRule & r1, const DottedRule & r2) const
  {
    return r1.symbol == r2.symbol && r1.expansion == r2.expansion && r1.dot_position == r2.dot_position;
  }
};


...
...
...
class BPCFG {

  public:


...
...
...
...

unordered_map<DottedRule, int, hash<DottedRule>, eqDottedRule> symbol_after_dot;

...
...
};

The last line I included is the only place where hash is used. Any idea what may be going on?

Thanks a lot, Onur

+1  A: 

From www.sgi.com: "The hash template is only defined for template arguments of type char*, const char*, crope, wrope, and the built-in integral types. If you need a Hash Function with a different argument type, you must either provide your own template specialization or else use a different Hash Function."

I'm pretty sure you need to define a std:size_t hash_value(DottedRule const&) function, and then you'll be able to use hash<DottedRule>. See the boost docs for more info.

Donnie DeBoer
Thank you! Specializing std::hash worked perfectly.
Onur Cobanoglu