sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Gaijinco's Avatar

Iterators within templates


Question posted by: Gaijinco (Guest) on March 26th, 2006 12:25 PM
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 Answers Posted
TB's Avatar
Guest - n/a Posts
#2: 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
John Carson's Avatar
Guest - n/a Posts
#3: 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


 
Not the answer you were looking for? Post your question . . .
196,998 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,998 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors