【发布时间】:2011-10-15 23:47:46
【问题描述】:
我有以下 C++ 代码:
#include <iostream>
#include <google/dense_hash_map>
#include <string.h>
using google::dense_hash_map; // namespace where class lives by default
using std::cout;
using std::endl;
using std::tr1::hash; // or __gnu_cxx::hash, or maybe tr1::hash, depending on your OS
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return (s1 == s2) || (s1 && s2 && strcmp(s1, s2) == 0);
}
};
int main(void){
dense_hash_map<const int, const char*, hash<const int>, eqstr> months;
months.set_empty_key(0);
months[1234] = "1234";
cout << "1234:" << months[1234] << endl;
}
如您所见,我正在使用 Google 的 sparsehash 库实现一个哈希表。
我使用整数作为键,使用字符串作为值。
但我不断收到以下编译错误,我无法深入了解:
制作所有构建文件:../src/Main.cpp 调用:GCC C++ 编译器 g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Main.d" -MT"src/Main.d" -o"src/Main.o" "../src/Main.cpp" 在包含的文件中 /usr/local/include/google/dense_hash_map:104:0, 来自../src/Main.cpp:2: /usr/local/include/google/sparsehash/densehashtable.h:在成员中 函数‘bool google::dense_hashtable::KeyInfo::equals(const key_type&, const key_type&) const [with Value = std::pair, Key = int, HashFcn = std::tr1::hash, ExtractKey = 谷歌::dense_hash_map, eqstr>::SelectKey, SetKey = google::dense_hash_map, eqstr>::SetKey, EqualKey = eqstr, Alloc = google::libc_allocator_with_realloc
, key_type = int]': /usr/local/include/google/sparsehash/densehashtable.h:1306:32:
从‘bool google::dense_hashtable::equals(const key_type&, const key_type&) const [with Value = std::pair, Key = int, HashFcn = std::tr1::hash, ExtractKey = 谷歌::dense_hash_map, eqstr>::SelectKey, SetKey = google::dense_hash_map, eqstr>::SetKey, EqualKey = eqstr, Alloc = 谷歌::libc_allocator_with_realloc , key_type = int]' /usr/local/include/google/sparsehash/densehashtable.h:514:5:
从 'void google::dense_hashtable::set_empty_key(google::dense_hashtable::const_reference) [with Value = 标准::对,键 = int,HashFcn = std::tr1::hash, ExtractKey = google::dense_hash_map, eqstr>::SelectKey, SetKey = 谷歌::dense_hash_map, eqstr>::SetKey, EqualKey = eqstr, Alloc = 谷歌::libc_allocator_with_realloc , google::dense_hashtable::const_reference = const std::pair&]' /usr/local/include/google/dense_hash_map:298:5:
从 'void google::dense_hash_map::set_empty_key(google::dense_hash_map::key_type&) [with Key = int, T = const char*, HashFcn = std::tr1::hash, EqualKey = eqstr, Alloc = 谷歌::libc_allocator_with_realloc , 谷歌::dense_hash_map::key_type = int]' ../src/Main.cpp:21:25: 从这里实例化 /usr/local/include/google/sparsehash/densehashtable.h:1293:39:错误: 来自‘google::dense_hashtable, int, std::tr1::hash, google::dense_hash_map, eqstr, 的无效转换, google::libc_allocator_with_realloc::SelectKey, eqstr, 谷歌::libc_allocator_with_realloc , 谷歌::dense_hash_map, eqstr, google::libc_allocator_with_realloc > >::SetKey, eqstr, 谷歌::libc_allocator_with_realloc , eqstr, google::libc_allocator_with_realloc > >::key_type' 到 'const char*' /usr/local/include/google/sparsehash/densehashtable.h:1293:39:错误: 初始化'bool eqstr::operator()(const char*, const的参数1 字符*) 常量' /usr/local/include/google/sparsehash/densehashtable.h:1293:39:错误: 来自‘google::dense_hashtable, int, std::tr1::hash, google::dense_hash_map, eqstr, 的无效转换, 谷歌::libc_allocator_with_realloc ::选择键,eqstr, 谷歌::libc_allocator_with_realloc , 谷歌::dense_hash_map, eqstr, google::libc_allocator_with_realloc > >::SetKey, eqstr, 谷歌::libc_allocator_with_realloc , eqstr, google::libc_allocator_with_realloc > >::key_type' 到 'const char*' /usr/local/include/google/sparsehash/densehashtable.h:1293:39:错误: 初始化'bool eqstr::operator()(const char*, const的参数2 char*) const' make: * [src/Main.o] 错误 1
这似乎很冗长,我无法理解它。
我应该补充一点,当我使用字符串作为键和整数作为值时,它可以正常工作:
dense_hash_map<const char*, int, hash<const char*>, eqstr> months;
...
months["february"] = 2; //works fine
有人有什么想法吗?
非常感谢,
编辑:现在可以使用了!
#include <iostream>
#include <google/dense_hash_map>
#include <string.h>
using google::dense_hash_map; // namespace where class lives by default
using std::cout;
using std::endl;
using std::tr1::hash; // or __gnu_cxx::hash, or maybe tr1::hash, depending on your OS
struct eqstr
{
bool operator()(int s1, int s2) const
{
return (s1 == s2); //|| (s1 && s2 && strcmp(s1, s2) == 0);
}
};
int main(void){
dense_hash_map<int, const char*, hash<int>, eqstr> months;
months.set_empty_key(0);
months[1234] = "1234";
cout << "1234:" << months[1234] << endl;
}
完全忘记了编辑 eqstr 结构以适应新的数据类型... 砰的一声离开办公桌
【问题讨论】:
-
抱怨是eqstr应该期待key类型,key类型不是const char*。根据您的声明,我对类型的了解不够多,无法说出密钥类型是什么(无符号整数?)。
标签: c++ compiler-errors hashtable