gpt4 book ai didi

c++ - g++ 链接器错误:获取 std::hash 的 undefined reference 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:41:01 25 4
gpt4 key购买 nike

我在我的代码中使用了 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' 的 undefined reference

我猜是关于哈希的使用。现在,整个代码太大了(如果你仍然想看它,我可以稍后发布),但相关部分是:

# 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;

...
...
};

我包含的最后一行是唯一使用散列的地方。知道会发生什么吗?

非常感谢,奥努尔

最佳答案

来自 www.sgi.com :“散列模板仅针对 char*、const char*、crope、wrope 和内置整数类型的模板参数定义。如果您需要具有不同参数类型的散列函数,则必须提供自己的模板特化或使用不同的哈希函数。”

我很确定你需要定义一个 std:size_t hash_value(DottedRule const&)功能,然后你就可以使用hash<DottedRule> .查看boost docs了解更多信息。

关于c++ - g++ 链接器错误:获取 std::hash 的 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1891242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com