Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6225795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:54:33+00:00 2026-05-24T08:54:33+00:00

I have the following C++ code: #include <iostream> #include <google/dense_hash_map> #include <string.h> using google::dense_hash_map;

  • 0

I have the following C++ code:

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

As you can see, I’m implementing a hash table using Google’s sparsehash library.

I am using integers as the key and strings as the value.

But I keep getting the following compilation error that I can’t get to the bottom of:

make all Building file: ../src/Main.cpp Invoking: 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” In file included from
/usr/local/include/google/dense_hash_map:104:0,
from ../src/Main.cpp:2:
/usr/local/include/google/sparsehash/densehashtable.h: In member
function ‘bool google::dense_hashtable::KeyInfo::equals(const key_type&,
const key_type&) const [with Value = std::pair, Key = int, HashFcn = std::tr1::hash, ExtractKey =
google::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:
instantiated from ‘bool google::dense_hashtable::equals(const key_type&, const
key_type&) const [with Value = std::pair, Key
= int, HashFcn = std::tr1::hash, ExtractKey =
google::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:514:5:
instantiated from ‘void google::dense_hashtable::set_empty_key(google::dense_hashtable::const_reference) [with Value =
std::pair, Key = int, HashFcn =
std::tr1::hash, ExtractKey = google::dense_hash_map, eqstr>::SelectKey, SetKey =
google::dense_hash_map,
eqstr>::SetKey, EqualKey = eqstr, Alloc =
google::libc_allocator_with_realloc
, google::dense_hashtable::const_reference = const std::pair&]’ /usr/local/include/google/dense_hash_map:298:5:
instantiated from ‘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 =
google::libc_allocator_with_realloc
, google::dense_hash_map::key_type
= int]’ ../src/Main.cpp:21:25: instantiated from here
/usr/local/include/google/sparsehash/densehashtable.h:1293:39: error:
invalid conversion from ‘google::dense_hashtable, int, std::tr1::hash, google::dense_hash_map, eqstr,
google::libc_allocator_with_realloc

::SelectKey, eqstr,
google::libc_allocator_with_realloc
, google::dense_hash_map,
eqstr, google::libc_allocator_with_realloc > >::SetKey, eqstr,
google::libc_allocator_with_realloc
, eqstr, google::libc_allocator_with_realloc > >::key_type’ to ‘const char*’
/usr/local/include/google/sparsehash/densehashtable.h:1293:39: error:
initializing argument 1 of ‘bool eqstr::operator()(const char*, const
char*) const’
/usr/local/include/google/sparsehash/densehashtable.h:1293:39: error:
invalid conversion from ‘google::dense_hashtable, int, std::tr1::hash, google::dense_hash_map, eqstr,
google::libc_allocator_with_realloc
::SelectKey, eqstr,
google::libc_allocator_with_realloc
, google::dense_hash_map,
eqstr, google::libc_allocator_with_realloc > >::SetKey, eqstr,
google::libc_allocator_with_realloc
, eqstr, google::libc_allocator_with_realloc > >::key_type’ to ‘const char*’
/usr/local/include/google/sparsehash/densehashtable.h:1293:39: error:
initializing argument 2 of ‘bool eqstr::operator()(const char*, const
char*) const’ make: * [src/Main.o] Error 1

It seems very verbose and I can’t make any sense of it.

I should add that when I use strings as the key and integers as the value, it works fine:

dense_hash_map<const char*, int, hash<const char*>, eqstr> months;
...
months["february"] = 2;   //works fine

Anyone got any ideas?

Many thanks in advance,


Edit: got it working now!

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

Totally forgot about editing the eqstr struct to accomodate the new data types… bangs head off desk

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:54:34+00:00Added an answer on May 24, 2026 at 8:54 am

    As you pointed out by yourself, it works if you use const char* as key. The hashmap indeed requires a hashing function to hash the keys to buckets and a comparison function to establish a strict weak ordering within the buckets – all this for the key type, the value type is just stored! So to make it work, define a comparison function for int (I don’t know whether const int is fine with google::dense_hash_map, I think it should be int).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
I have the following code compiled by gcc: #include <iostream> using namespace std; class
I have following code #include <iostream> #include <string> #include <algorithm> using namespace std; int
i have following code #include <iostream> #include <utility> using namespace std; namespace rel_ops{ template<class
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
I have the following code #include <iostream> using namespace std; class Object { public:
I have the following code #include <iostream> #include<string> #include <sstream> using namespace std; struct
i have following code #include <iostream> #include <string> using namespace std; string generate(){ for
I have the following code: #include <iostream> #include <string> #include <unistd.h> using namespace std;
I have the following code: #include <iostream> using namespace std; class testing{ int test()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.