c++ - std library map and template functions -
this question has answer here:
i don't understand why code
#include <map> template<typename t, typename u> std::ostream& operator<<(std::ostream& o, const std::map<t,u>& input) { (std::map<typename t,typename u>::iterator it=input.begin(); it!=input.end(); ++it) { o << it->first << " => " << it->second << '\n'; } return o; }
returns compilation error:
error: wrong number of template arguments (1, should 4) error: provided ‘template<class _key, class _tp, class _compare, class _alloc> class std::map’
can me please??
you should write typename before iterator declaration , use const_iterator:
for (typename std::map<t,u>::const_iterator it=input.begin(); it!=input.end(); ++it
the argument of operator << requires const objects. elements of map must const. achieve 1 uses const_iterator. typename in declaration of iterator required indicate following expression nested template class depending on types t , u.
see question: making sense of arguments function template
Comments
Post a Comment