C ++: hashtable - Почему это не компилируется?

У меня есть следующий код 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.

Я использую целые числа в качестве ключа и строки в качестве значения.

Но я продолжаю получать следующую ошибку компиляции, до которой не могу разобраться:

make all Building file: ../src/Main.cpp Вызов: GCC C ++ Compiler 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 / density_hash_map: 104: 0, из ../src/Main.cpp:2: /usr/local/include/google/sparsehash/densehashtable.h: в члене функция ‘bool google :: density_hashtable :: KeyInfo :: equals (const key_type &, const key_type &) const [со значением = std :: pair, Key = int, HashFcn = std :: tr1 :: hash, ExtractKey = google :: density_hash_map, eqstr> :: SelectKey, SetKey = google :: density_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 :: density_hashtable :: equals (const key_type &, const key_type &) const [со значением = std :: pair, ключом = int, HashFcn = std :: tr1 :: hash, ExtractKey = google :: density_hash_map, eqstr> :: SelectKey, SetKey = google :: density_hash_map, eqstr> :: SetKey, EqualKey = eqstr, Alloc = google :: libc_allocator_with_realloc , key_type = int] ’ /usr/local/include/google/sparsehash/densehashtable.h:514:5:
создается из "void google :: density_hashtable :: set_empty_key (google :: density_hashtable :: const_reference)" [со значением = std :: pair, ключ = int, HashFcn = std :: tr1 :: hash, ExtractKey = google :: density_hash_map, eqstr> :: SelectKey, SetKey = google :: density_hash_map, eqstr> :: SetKey, EqualKey = eqstr, Alloc = google ::libc_allocator_with_realloc , google :: density_hashtable :: const_reference = const std :: pair &] ’/ usr / local / include / google / density_hash_map: 298: 5:
создается из ‘void google :: density_hash_map :: set_empty_key (google :: density_hash_map :: key_type &) [с Key = int, T = const char *, HashFcn = std :: tr1 :: hash, EqualKey = eqstr, Alloc = google :: libc_allocator_with_realloc , google :: density_hash_map :: key_type = int] ’../src/Main.cpp:21:25: создан отсюда /usr/local/include/google/sparsehash/densehashtable.h:1293:39: ошибка: недопустимое преобразование из google :: density_hashtable, int, std :: tr1 :: hash, google :: density_hash_map, eqstr, google :: libc_allocator_with_realloc

:: SelectKey, eqstr, google :: libc_allocator_with_realloc , google :: density_hash_map, eqstr, google :: libc_allocator_with_realloc>> :: SetKey, eqstr, google :: libc_allocator_with_realloc , eqstr, google :: libc_allocator_with_realloc>> :: key_type ’на‘ const char * ’ /usr/local/include/google/sparsehash/densehashtable.h:1293:39: ошибка: инициализация аргумента 1 ‘bool eqstr :: operator () (const char *, const char *) const ’ /usr/local/include/google/sparsehash/densehashtable.h:1293:39: ошибка: недопустимое преобразование из google :: density_hashtable, int, std :: tr1 :: hash, google :: density_hash_map, eqstr, google :: libc_allocator_with_realloc :: SelectKey, eqstr, google :: libc_allocator_with_realloc , google :: density_hash_map, eqstr, google :: libc_allocator_with_realloc>> :: SetKey, eqstr, google :: libc_allocator_with_realloc , eqstr, google :: libc_allocator_with_realloc>> :: key_type ’на‘ const char * ’ /usr/local/include/google/sparsehash/densehashtable.h:1293:39: ошибка: инициализация аргумента 2 ‘bool eqstr :: operator () (const char *, const 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 структура для размещения новых типов данных ... бьет головой

5
задан Eamorr 29 July 2011 в 13:52
поделиться