compiler errors on iterator

F

frankz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top