Template problem with unordered_map

P

Paulo Matos

HI all,

I'm using unordered_map from TR1.

I have a table variable which is a private member of a template class:

template<typename K, typename D, typename EQ>
class FooClass {
public:

void method(K& key) {
const unordered_map<K, D, hash<K>, EQ>::const_iterator it =
table.find(key);
....
}

private:
unordered_map<K, D, hash<K>, EQ> table;
};

I get an error in the const_iterator line:
error: expected initializer before 'it'

Any ideas on what the problem might be?
I think I have all the includes and using keywords ok (on top of file):
#include <tr1/unordered_map>

using std::tr1::unordered_map;
using std::tr1::hash;


Any ideas?

Cheers,

Paulo Matos
 
H

Howard Hinnant

"Paulo Matos said:
HI all,

I'm using unordered_map from TR1.

I have a table variable which is a private member of a template class:

template<typename K, typename D, typename EQ>
class FooClass {
public:

void method(K& key) {
const unordered_map<K, D, hash<K>, EQ>::const_iterator it =
table.find(key);
...
}

private:
unordered_map<K, D, hash<K>, EQ> table;
};

I get an error in the const_iterator line:
error: expected initializer before 'it'

Any ideas on what the problem might be?
I think I have all the includes and using keywords ok (on top of file):
#include <tr1/unordered_map>

using std::tr1::unordered_map;
using std::tr1::hash;


Any ideas?

Try:

const typename unordered_map<K, D, hash<K>, EQ>::const_iterator it =

-Howard
 
P

Paulo Matos

Howard said:
Try:

const typename unordered_map<K, D, hash<K>, EQ>::const_iterator it =

Hi,

Thanks a lot, why does that work?
Why do I need typename?
 
T

tragomaskhalos

Paulo said:
Hi,

Thanks a lot, why does that work?
Why do I need typename?

The compiler doesn't know a priori what sort of a beast this
"const_iterator" in "unordered_map<K, D, hash<K>, EQ>" might be - maybe
it's a static int for example, in which case your expression is
invalid. The typename indicates that it's, well, a typename, so that
the expression "const Thingy it = Yadda" now makes sense.
 
P

Paulo Matos

tragomaskhalos said:
The compiler doesn't know a priori what sort of a beast this
"const_iterator" in "unordered_map<K, D, hash<K>, EQ>" might be - maybe
it's a static int for example, in which case your expression is
invalid. The typename indicates that it's, well, a typename, so that
the expression "const Thingy it = Yadda" now makes sense.

Yes, thanks a lot. Also found an in-depth explanation in "Effective
C++" Item 42! :)
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top