Re: overloading << for map and multimap simultaneously
ozizus wrote:
I overloaded operator << for STL map successfully:
template <typename T1, typename T2> ostream & operator << (ostream &
o, map <T1,T2> & m)
{
//code
}
the code works like a charm. Now, I want the same functionality for
multimap. Since their interface is same for the problem at hand, I
want to make collection name a template parameter like this:
template <typename MAP, typename T1, typename T2> ostream & operator
<< (ostream & o, MAP<T1,T2> & m)
{
//code
}
The compiler errors like this: syntax error : missing ')' before '<'
The compiler cannot deduce all three from the argument you give.
Besides, if 'MAP' is a template (as you imply by using the template
syntax when declaring the second argument), then it's not a "typename"
argument, it's a "template <class,class> class MAP" argument. Try
template<template<class, class> class MAP, typename T1, typename T2>
ostream& operator << (...
but it is most likely not going to work. I recommend dropping the T1
and T2 arguments and just do
template<class MAP> ostream& operator <<( ostream& o, MAP const& m)
(you do want to have the output object 'const', by the way).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask