hash_map woes

Started by
4 comments, last by mike74 18 years, 9 months ago
I'm creating a skeletal animation system, and I want to create a map between a point in its initial position and a point in its transformed position. So, I have: struct mikepoint { float x, y, z; public: BOOL operator==(mikepoint &p1) {return (x==p1.x && y==p1.y && z==p1.z);} }; I've never really used a hash_map in C++ before, but I thought I'd try it: hash_map<mikepoint,mikepoint> allpointsmap; Then, I have a mikepoint called v0. Just for testing, I tried: allpointsmap[v0] = v0; The compiler got mad, and said: 200 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_function.h no match for 'operator==' in '__x == __y' note C:\Dev-Cpp\linedraw.cpp:25 candidates are: BOOL mikepoint::operator==(mikepoint&) note C:\Dev-Cpp\linedraw.cpp:25 BOOL operator==(const GUID&, const GUID&) What's up with this? I added the operator== function to try to make it work, but that didn't seem to do it. Mike C. http://www.coolgroups.com/zoomer
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
Advertisement
Try changing your operator==() to take a const mikepoint &.
Not only that it should be a constant member function. Also you will need to define a hashing function for user-defined key types. Which compiler are using? because not all hash_map are equal as its a library extension and not currently part of the standard.

Oh don't use BOOL use bool, BOOL is an MS type alias, your declaration should like this:

bool operator==(const mikepoint&) const;
Ok, thanks. Can you help me define the hash function? I can't figure out how to make the prototype even. I guess it's something like

int hash(mikepoint m)

but that doesn't quite work.

Mike C.
http://www.coolgroups.com/zoomer

Quote:Original post by snk_kid
Not only that it should be a constant member function. Also you will need to define a hashing function for user-defined key types. Which compiler are using? because not all hash_map are equal as its a library extension and not currently part of the standard.

Oh don't use BOOL use bool, BOOL is an MS type alias, your declaration should like this:

bool operator==(const mikepoint&) const;


Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
Quote:Original post by mike74
Ok, thanks. Can you help me define the hash function? I can't figure out how to make the prototype even. I guess it's something like

int hash(mikepoint m)

but that doesn't quite work.


which compiler are you using?

okay looking at the error messages its GCC [grin], you can either do a full specialization of hash or provide your own hash type.

To do a specialization of hash:

#include <ext/hash_map>namespace __gnu_cxx {   template <>   struct hash< const mikepoint > {      size_t operator()(const mikepoint& m_ref) const  {          /* insert hashing code */      }   };   template <>   struct hash< mikepoint > : hash< const mikepoint > {};};


To provide a custom hash type, just write a functor which takes a constant reference to mikepoint and returns std::size_t.
It seems to work. Thanks a lot!

Mike C.
http://www.coolgroups.com/zoomer
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/

This topic is closed to new replies.

Advertisement