Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   compiler errors on iterator (http://www.velocityreviews.com/forums/t731104-compiler-errors-on-iterator.html)

frankz 08-16-2010 03:21 PM

compiler errors on iterator
 
Below is a piece of code that I abstracted from a C++ book but doesn't
compile. If I substitute template class T with int type, it compiles.
Don't know why?

#include <iostream>

#include <iterator> // for iterator

using std::cout;

using std::cin;

using std::endl;





#include <vector>



template <class T>

void printVector( const std::vector<T> &vec)

{

std::vector<T>::const_iterator i;

for (i=vec.begin(); i!= vec.end();i++)

cout << *i << " ";

}

g++ -Wall -o "stl" "stl.cpp"
stl.cpp: In function ‘void printVector(const std::vector<T,
std::allocator<_CharT> >&)’:
stl.cpp:13: error: expected ‘;’ before ‘i’
stl.cpp:14: error: ‘i’ was not declared in this scope
Compilation failed.


frankz 08-16-2010 05:52 PM

Re: compiler errors on iterator
 
On Aug 16, 11:25*am, "Leigh Johnston" <le...@i42.co.uk> wrote:
> "frankz" <frank.zhu.min...@gmail.com> wrote in message
>
> news:58254fa8-fc62-4162-b3ae-4d0161d9c442@h25g2000vba.googlegroups.com...
>
>
>
> > Below is a piece of code that I abstracted from a C++ book but doesn't
> > compile. If I substitute template class T with int type, it compiles.
> > Don't know why?

>
> > #include <iostream>

>
> > #include <iterator> // for iterator

>
> > using std::cout;

>
> > using std::cin;

>
> > using std::endl;

>
> > #include <vector>

>
> > template <class T>

>
> > void printVector( const std::vector<T> &vec)

>
> > {

>
> > * *std::vector<T>::const_iterator i;

>
> > * *for (i=vec.begin(); i!= vec.end();i++)

>
> > * * * *cout << *i << " ";

>
> > }

>
> > g++ -Wall -o "stl" "stl.cpp"
> > stl.cpp: In function ‘void printVector(const std::vector<T,
> > std::allocator<_CharT> >&)’:
> > stl.cpp:13: error: expected ‘;’ before ‘i’
> > stl.cpp:14: error: ‘i’ was not declared in this scope
> > Compilation failed.

>
> Try
>
> typename std::vector<T>::const_iterator i;
>
> /Leigh


Thanks a lot! Can you explain why? btw The original code is from "C++
How to program"

red floyd 08-16-2010 06:19 PM

Re: compiler errors on iterator
 
On 8/16/2010 10:52 AM, frankz wrote:
> On Aug 16, 11:25 am, "Leigh Johnston"<le...@i42.co.uk> wrote:
>> "frankz"<frank.zhu.min...@gmail.com> wrote in message
>>
>> news:58254fa8-fc62-4162-b3ae-4d0161d9c442@h25g2000vba.googlegroups.com...
>>
>>
>>
>>> Below is a piece of code that I abstracted from a C++ book but doesn't
>>> compile. If I substitute template class T with int type, it compiles.
>>> Don't know why?

>>
>>> #include<iostream>

>>
>>> #include<iterator> // for iterator

>>
>>> using std::cout;

>>
>>> using std::cin;

>>
>>> using std::endl;

>>
>>> #include<vector>

>>
>>> template<class T>

>>
>>> void printVector( const std::vector<T> &vec)

>>
>>> {

>>
>>> std::vector<T>::const_iterator i;

>>
>>> for (i=vec.begin(); i!= vec.end();i++)

>>
>>> cout<< *i<< " ";

>>
>>> }

>>
>>> g++ -Wall -o "stl" "stl.cpp"
>>> stl.cpp: In function ‘void printVector(const std::vector<T,
>>> std::allocator<_CharT> >&)’:
>>> stl.cpp:13: error: expected ‘;’ before ‘i’
>>> stl.cpp:14: error: ‘i’ was not declared in this scope
>>> Compilation failed.

>>
>> Try
>>
>> typename std::vector<T>::const_iterator i;
>>
>> /Leigh

>
> Thanks a lot! Can you explain why? btw The original code is from "C++
> How to program"


Yes, It's a FAQ. Please see the FAQ, in particular FAQ 35.18

http://www.parashift.com/c++-faq-lite


All times are GMT. The time now is 11:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57