I can easily write a pretty-printer for std::vector<*> like this:
template <typename T>
std::ostream &operator<<(std::ostream &origin, const std::vector<T> &vec){
origin << "{ ";
for(int i = 0; i < vec.size(); i++)
origin << vec[i] << " ";
origin << "}";
return origin;
}
But when I wrote a pretty-printer for std::list<*> similarly:
template <typename T>
std::ostream &operator<<(std::ostream &origin, const std::list<T> &lis){
origin << "[ ";
for(std::list<T>::const_iterator it = lis.begin(); it != lis.end(); it++)
origin << *it << " ";
origin << "]";
return origin;
}
gcc reported:
../main.cpp: In function ‘std::ostream& operator<<(std::ostream&, const std::list<T>&)’:
../main.cpp:13:6: error: need ‘typename’ before ‘std::list<T>::const_iterator’ because ‘std::list<T>’ is a dependent scope
for(std::list<T>::const_iterator it = lis.begin(); it != lis.end(); it++)
^
../main.cpp:13:35: error: expected ‘;’ before ‘it’
for(std::list<T>::const_iterator it = lis.begin(); it != lis.end(); it++)
^
../main.cpp:13:53: error: ‘it’ was not declared in this scope
for(std::list<T>::const_iterator it = lis.begin(); it != lis.end(); it++)
^
../main.cpp: In instantiation of ‘std::ostream& operator<<(std::ostream&, const std::list<T>&) [with T = int; std::ostream = std::basic_ostream<char>]’:
../main.cpp:24:15: required from here
../main.cpp:13:51: error: dependent-name ‘std::list<T>::const_iterator’ is parsed as a non-type, but instantiation yields a type
for(std::list<T>::const_iterator it = lis.begin(); it != lis.end(); it++)
^
../main.cpp:13:51: note: say ‘typename std::list<T>::const_iterator’ if a type is meant
make: *** [main.o] Error 1
Could you help me writing a working pretty-printer for std::list<*> and explain the meaning of the error message to me please?