Iterators within templates

G

Gaijinco

Why this doesn't work?

1: template <typename T>
2: void imprimir(vector<T> v)
3: {
4: for(vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)
5: cout << *pos << " ";
6: cout << endl;
7: }
8:

The compiler says
Line 4 expected `;' before "pos"

I don't know what that's supposed to mean! Thanks.
 
T

TB

Gaijinco skrev:
Why this doesn't work?

1: template <typename T>
2: void imprimir(vector<T> v)
3: {
4: for(vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)
 
J

John Carson

Gaijinco said:
Why this doesn't work?

1: template <typename T>
2: void imprimir(vector<T> v)
3: {
4: for(vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)
5: cout << *pos << " ";
6: cout << endl;
7: }
8:

The compiler says
Line 4 expected `;' before "pos"

I don't know what that's supposed to mean! Thanks.

Please don't include line numbers with your code. Anyone who attempts to
compile it must first manually delete the numbers.

Replace

for(vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)

with

for(typename vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)

The compiler is interpreting vector<T>::iterator as a variable name, not as
a type name. Using typename tells it to interpret it as a type.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top