Sign in to follow this  
RegularKid

STL hash_map errors...

Recommended Posts

Hi, I'm trying to use an STL hash_map in my project ( on a Mac using XCode ) and I'm getting some compiler errors when attempting to use the hash_map function "find". Here is how I'm declaring my hash_map:
hash_map<double, MyClass*> myHash;

I can then add elements into the hash like so without any problems:
myHash[ 0 ] = new MyClass();
myHash[ 5 ] = new MyClass();

This all works fine, but when I try to use the find() function like this:
if( myHash.find( 3 ) == myHash.end() )
{
    printf( "Couldn't find hash entry!\n");
}

Then I get the following errors when I compile: "error: no match for call to '(const __gnu_cxx::hash<double>)(const double&)'" I'm not sure what's going on. I tried even adding my own hash function for it like this:
struct eqdouble
{
    bool operator()(double s1, double s2) const
    {
        return s1 == s2;
    }
};
hash_map<double, MyClass*, hash<double>, eqdouble> myHash;

However, that didn't seem to do anything different ( I'm assuming there is already a built in hash function for a double ). Any help would be great! Thanks!

Share this post


Link to post
Share on other sites
Just to chime in on C++ and floats/doubles, if you end up doing any math on them they are not gonna give you exact results which may make it difficult to accurately get the same value over and over for retrieval. Unless you really need it I would use an int as your key value.

Honestly unless the speed is a huge issue I would just use std::map.
BTW hash_map is no longer part of the STL I think its part of stdext or something similar.

I know neither of those comments cleared up your compiler error. To me it looks like there is no hashing function for double that the compiler can find, again I recommend trying int.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this