Sign in to follow this  
mike74

hash_map woes

Recommended Posts

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

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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;


Share this post


Link to post
Share on other sites
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.

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