Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > compiler errors on iterator

Reply
Thread Tools

compiler errors on iterator

 
 
frankz
Guest
Posts: n/a
 
      08-16-2010
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.

 
Reply With Quote
 
 
 
 
frankz
Guest
Posts: n/a
 
      08-16-2010
On Aug 16, 11:25*am, "Leigh Johnston" <(E-Mail Removed)> wrote:
> "frankz" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > 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"
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      08-16-2010
On 8/16/2010 10:52 AM, frankz wrote:
> On Aug 16, 11:25 am, "Leigh Johnston"<(E-Mail Removed)> wrote:
>> "frankz"<(E-Mail Removed)> wrote in message
>>
>> news:(E-Mail Removed)...
>>
>>
>>
>>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments