When I use the template as below, the g++ will report error:
E32.cpp: In function ‘void display_vector(const std::vector&, std::ostream&, int)’:
E32.cpp:21:5: error: need ‘typename’ before ‘std::vector::const_iterator’ because ‘std::vector’ is a dependent scope vector::const_iterator ^
template <typename elemType>
void display_vector(const vector<elemType> &vec,
ostream &os=cout, int len=8)
{
vector<elemType>::const_iterator
iter = vec.begin(),
end_it = vec.end();
int elem_cnt = 1;
while(iter != end_it)
{
os << *iter++ << (!(elem_cnt++ % len)?'\n':' ');
}
os << endl;
}
Why? I can't figure it out...