4

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

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
Onur Cobanoglu
  • 211
  • 1
  • 6
  • 9
  • In standard C++ hash is not declared in the std namespace. But unless you post more code, it's difficult to say if this is an issue or not. Also, depending on what you are doing should be –  Dec 11 '09 at 22:41

2 Answers2

6

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
  • 2,517
  • 15
  • 14
0

Simple hash for my class. It cals hash from string

namespace std
{
template<>
struct hash<Letter> : public __hash_base<size_t, Letter>
{
    size_t operator()(const Letter& v) const
    {
        hash<string> hasher;
        return hasher.operator ()(v.getSign());
    }
};
}
Gelldur
  • 11,187
  • 7
  • 57
  • 68