Connecting Tech Pros Worldwide Help | Site Map

Iterators within templates

  #1  
Old March 26th, 2006, 12:25 PM
Gaijinco
Guest
 
Posts: n/a
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.

  #2  
Old March 26th, 2006, 12:55 PM
TB
Guest
 
Posts: n/a

re: Iterators within templates


Gaijinco skrev:[color=blue]
> 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)[/color]

for(typename vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)
[color=blue]
> 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.
>[/color]

--
TB @ SWEDEN
  #3  
Old March 26th, 2006, 01:45 PM
John Carson
Guest
 
Posts: n/a

re: Iterators within templates


"Gaijinco" <gaijinco@gmail.com> wrote in message
news:1143375411.571278.160440@u72g2000cwu.googlegr oups.com[color=blue]
> 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.[/color]

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.

--
John Carson


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are C++ templates a precompiler thing? JohnQ answers 104 June 16th, 2007 12:35 AM
std::ios::binary? Steven T. Hatton answers 103 August 7th, 2005 03:05 PM
Alternatives to the C++ Standard Library? Steven T. Hatton answers 43 July 23rd, 2005 06:52 AM
Efficiency of map and vector iterators Jim West answers 14 July 22nd, 2005 05:26 AM