![]() |
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. |
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" |
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:22 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.