STL hash_map errors...

Started by
2 comments, last by vs322 15 years, 9 months ago
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!
Advertisement
Try:

if( myHash.find( 3.0 ) == myHash.end() ){    printf( "Couldn't find hash entry!\n");}
[size=2]Darwinbots - [size=2]Artificial life simulation
Hey,

Sorry, I should have been more clear in my original thread. I'm using a double variable to pass into find() ( i'm just using the 3 constant as an example ).
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.

This topic is closed to new replies.

Advertisement