g++链接器错误:获取 std::hash 的未定义引用错误
我在代码中使用 TR1 实现的 unordered_map ,链接器给出了奇怪的错误,我什至无法破译:
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
这就是错误,我什至无法检测到它所在的行?从声明中:
对`std::hash::operator()(DottedRule) const'的未定义引用
我猜想这是关于哈希的使用。现在,整个代码太大了(如果您仍然想看它,我可以稍后发布),但相关部分是:
# 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;
...
...
};
我包含的最后一行是唯一使用哈希的地方。知道会发生什么吗?
多谢, 奥努尔
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 www.sgi.com:“哈希模板仅为模板参数定义类型 char*、const char*、crope、wrope 和内置整数类型如果您需要具有不同参数类型的哈希函数,则必须提供自己的模板专业化,或者使用不同的哈希函数。”
我很确定您需要定义一个
std:size_t hash_value(DottedRule const&)
函数,然后您就可以使用hash
。有关详细信息,请参阅 boost 文档。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 usehash<DottedRule>
. See the boost docs for more info.我的班级的简单哈希。它从字符串调用哈希
Simple hash for my class. It cals hash from string