473,372 Members | 814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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

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
3 2919
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Wang, Jay | last post by:
Hello, all, I would like to enable some text between <SPAN url="http://www.testserver.com/">WORD TO BE DRAGGED </SPAN>. I put some javascript and it will extract http://www.testserver.com/ from...
6
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this...
14
by: Schraalhans Keukenmeester | last post by:
I am building a default sheet for my linux-related pages. Since many linux users still rely on/prefer viewing textmode and unstyled content I try to stick to the correct html tags to pertain good...
10
by: g | last post by:
hello! I wanna replace an std::map<std::string,Services*> with hash_map.How I will do this? any link with examples? transactions.insert(std::pair<std::string,Services*>("Aservice",new...
5
by: Marc Gravell | last post by:
Short version: is it possible to control the endian-ness of BitConverter? - or are there any /framework/ methods like BigEndianBitConverter.GetBytes(long) and .ToInt64()? (I am not after...
17
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This...
13
by: Mark | last post by:
Dear folks, In Javascript, is it possible to get all id names within, say, a <div></divpair? Like the array of "document.images", I mean. The reason I ask, is that I have a calender whose...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
23
by: Xah | last post by:
Here's a interesting case of invalid html 4 strict file. In summary, if you have <body></bodywithout any content, the file would be invalid under html 4 strict. Valid if html 4 lose. <!DOCTYPE...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.