By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jun 30 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
newbie wrote:
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*,floatblah;
}

What happens?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 30 '07 #2

P: n/a
newbie wrote:
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
Jun 30 '07 #3

P: n/a
On Jun 30, 2:51 am, newbie <mitbb...@yahoo.comwrote:
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 30 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.