a very interesting problem of hash_map<> and map<>

N

newbie

Same thing g++ complains when using hash_map<>, but is happy with
map<> --I understand hahs_map is not standardized, but since the
compiler didn't complain something like 'hash_map<> not defined', I
suppose it's supported and should behave well when I used it
correctly. BUT it didn't.

Here is my code snippet:
class MyKey {
public:
virtual void foo() { return; }
...
}

class MyContainer {
public:
typedef hash_map<MyKey*, float> _elements; // (1) typedef
map<MyKey*, float> _elements;
void Add(MyKey* key, float value) { if (_elements.find(key) !=
_elements.end() ) _elements[key] = value; }
}

When I compile the above code, the compiler complains something like
prototype not matched; when I replace line(1) with 'typedef
map<MyKey*, float> _elements;', everything passed.

Can you see the problem?

thanks a lot
 
V

Victor Bazarov

newbie said:
Same thing g++ complains when using hash_map<>, but is happy with
map<> --I understand hahs_map is not standardized, but since the
compiler didn't complain something like 'hash_map<> not defined', I
suppose it's supported and should behave well when I used it
correctly. BUT it didn't.

Here is my code snippet:
class MyKey {
public:
virtual void foo() { return; }
...
}

class MyContainer {
public:
typedef hash_map<MyKey*, float> _elements; // (1) typedef
map<MyKey*, float> _elements;
void Add(MyKey* key, float value) { if (_elements.find(key) !=
_elements.end() ) _elements[key] = value; }
}

When I compile the above code, the compiler complains something like
prototype not matched; when I replace line(1) with 'typedef
map<MyKey*, float> _elements;', everything passed.

Can you see the problem?

No. Can you try

#include <hashmap> // or whatever you think is needed
int main()
{
hash_map<char*,float> blah;
}

What happens?

V
 
J

John Harrison

newbie said:
Same thing g++ complains when using hash_map<>, but is happy with
map<> --I understand hahs_map is not standardized, but since the
compiler didn't complain something like 'hash_map<> not defined', I
suppose it's supported and should behave well when I used it
correctly. BUT it didn't.

Here is my code snippet:
class MyKey {
public:
virtual void foo() { return; }
...
}

class MyContainer {
public:
typedef hash_map<MyKey*, float> _elements; // (1) typedef
map<MyKey*, float> _elements;
void Add(MyKey* key, float value) { if (_elements.find(key) !=
_elements.end() ) _elements[key] = value; }
}

When I compile the above code, the compiler complains something like
prototype not matched; when I replace line(1) with 'typedef
map<MyKey*, float> _elements;', everything passed.

Can you see the problem?

thanks a lot

Both versions are wrong. You define _elements as a type and then use it
as if it was a member variable. You are making a mistake somewhere by
NOT POSTING THE CODE YOU ARE ACTUALLY COMPILING. Sorry but its very
frustrating when people do this.

If you got rid of typedef then your code would make some sense.

john
 
J

James Kanze

Same thing g++ complains when using hash_map<>, but is happy with
map<> --I understand hahs_map is not standardized, but since the
compiler didn't complain something like 'hash_map<> not defined', I
suppose it's supported and should behave well when I used it
correctly.

Like most C++ compilers, g++ does support a hash_map container,
provided you include the right headers. Note, however, that
ther requirements for the key type are not the same as for
std::map; std::map requires that the key be "less than
comparable", where as hash_map requires equality and hashable.
If you've defined the six comparison operators for your key
type, the probable problem is that you haven't defined a hash
function for it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top