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.
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.