Cannot make const_iterator of templatized hash_map

J

Joseph Turian

In a class templatized by class T, I have the following two lines:

hash_map<string, T> foo;
hash_map<string, T>::const_iterator p;

The first line compiles just fine.
The second line gives the following error: "syntax error before `;'
token"

Any idea how to resolve this error?

Thanks,
Joseph
 
V

Victor Bazarov

Joseph said:
In a class templatized by class T, I have the following two lines:

hash_map<string, T> foo;
hash_map<string, T>::const_iterator p;

The first line compiles just fine.
The second line gives the following error: "syntax error before `;'
token"

Any idea how to resolve this error?

For dependent names, if they are types, add 'typename' to help your
compiler:

typename hash_map<string, T>::const_iterator p;

More about it in the FAQ.

V
 
J

Joseph Turian

Okay, I figured it out:
typename hash_map<string, T>::const_iterator p;

However, I have no idea why this works.

Joseph
 
J

Joseph Turian

Victor said:
Read the FAQ.

Victor,

Which question should I be looking for?
I searched the FAQ for typename, however I couldn't find the
appropriate question.

Thanks,

Joseph
 
V

Victor Bazarov

Joseph said:
Victor,

Which question should I be looking for?
I searched the FAQ for typename, however I couldn't find the
appropriate question.

I think section 35 should be of some assistance. Also, read the
archives about "dependent name" or "dependent type".

V
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top