473,666 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloading << for map and multimap simultaneously

I overloaded operator << for STL map successfully:

template <typename T1, typename T2ostream & 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 T2ostream & operator
<< (ostream & o, MAP<T1,T2& m)
{
//code
}

The compiler errors like this: syntax error : missing ')' before '<'

thx,
ozi.

Jul 29 '07 #1
10 1811
ozizus wrote:
I overloaded operator << for STL map successfully:

template <typename T1, typename T2ostream & 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 T2ostream & 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,classcla ss MAP" argument. Try

template<templa te<class, classclass 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 MAPostream& 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
Jul 29 '07 #2
ozizus wrote:
I overloaded operator << for STL map successfully:

template <typename T1, typename T2ostream & 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 T2ostream & operator
<< (ostream & o, MAP<T1,T2& m)
{
//code
}

The compiler errors like this: syntax error : missing ')' before '<'
a) technically, you want something like:

template < template <class, class class MAP, typename T1, typename T2 >

Read up on template template parameters (as opposed to typename template
parameters).

b) Using a template template parameter here, is a BadIdea(tm) since your
template would match too many things, e.g., it would match a pair<S,T>. In
that case, your code will not work.

Thus: you should just do another template for multimaps.

c) You may want to make your code aware of the Allocator and Comparison
typename parameters in the map template. I.e.,

template< typename S, typename T, typename A, typename C >
std::ostream & operator<< ( std::ostream & ostr,
const std::multimap< S, T, A, C & v );

Best

Kai-Uwe Bux
Jul 29 '07 #3
On Jul 29, 4:00 pm, ozizus <o...@ce.yildiz .edu.trwrote:
I overloaded operator << for STL map successfully:

template <typename T1, typename T2ostream & 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 T2ostream & operator
<< (ostream & o, MAP<T1,T2& m)
{
//code

}

The compiler errors like this: syntax error : missing ')' before '<'

thx,
ozi.
you can simply write:

template<typena me MapT>
ostream & operator << (ostream & o, const MapT& m){
typedef typename MapT::Key_type T1;
typedef typename MapT::mapped_ty pe T2;
//code

return o;
};
if your compiler supports concepts ,things will be easier :

auto concept map_like <MapT>{//what a (multi)map looks like.
typedef typename MapT::key_type key_type;
typedef typename MapT::mapped_ty pe mapped_type;
//etc:

};

template<map_li ke MapT>//argument type must behave like a map
ostream & operator << (ostream & o, const MapT& m){
//code

return o;
};
regards,
FM.

Jul 29 '07 #4
a) technically, you want something like:

template < template <class, class class MAP, typename T1, typename T2 >

Read up on template template parameters (as opposed to typename template
parameters).
I tried the code below, but got error C2065: 'T2' : undeclared
identifier.

template<templa te<class,classc lass MAP, typename T1, typename t2>
ostream & operator << (ostream & o, MAP<T1,T2& m)

C++ is huge, is not it, lots of reading up to do.. Thx anyway.

Jul 29 '07 #5
>
template<typena me MapT>
ostream & operator << (ostream & o, const MapT& m){
typedef typename MapT::Key_type T1;
typedef typename MapT::mapped_ty pe T2;
//code

return o;

};
template<typena me MapTostream & operator << (ostream & o, MapT & m)
{
typedef typename MapT::key_type T1;
typedef typename MapT::mapped_ty pe T2;
...
}

gives the cute one liner below:

error LNK2019: unresolved external symbol "class
std::basic_ostr eam<char,struct std::char_trait s<char & __cdecl
operator<<<clas s std::vector<int ,class std::allocator< int,class
std::map<class std::vector<int ,class std::allocator< int,int,struct
std::less<class std::vector<int ,class std::allocator< int >,class
std::allocator< struct std::pair<class std::vector<int ,class
std::allocator< int const ,int (class
std::basic_ostr eam<char,struct std::char_trait s<char &,class
std::map<class std::vector<int ,class std::allocator< int,class
std::map<class std::vector<int ,class std::allocator< int,int,struct
std::less<class std::vector<int ,class std::allocator< int >,class
std::allocator< struct std::pair<class std::vector<int ,class
std::allocator< int const ,int >,struct std::less<class
std::vector<int ,class std::allocator< int >,class
std::allocator< struct std::pair<class std::vector<int ,class
std::allocator< int const ,class std::map<class
std::vector<int ,class std::allocator< int,int,struct std::less<class
std::vector<int ,class std::allocator< int >,class
std::allocator< struct std::pair<class std::vector<int ,class
std::allocator< int const ,int &)" (??$?6V?$vector @HV?
$allocator@H@st d@@@std@@V?$map @V?$vector@HV?$ allocator@H@std @@@std@@HU?
$less@V?$vector @HV?$allocator@ H@std@@@std@@@2 @V?$allocator@U ?$pair@$
$CBV?$vector@HV ?$allocator@H@s td@@@std@@H@std @@@2@@1@@@YAAAV ?
$basic_ostream@ DU?$char_traits @D@std@@@std@@A AV01@AAV?$map@V ?
$vector@HV?$all ocator@H@std@@@ std@@V?$map@V?$ vector@HV?
$allocator@H@st d@@@std@@HU?$le ss@V?$vector@HV ?
$allocator@H@st d@@@std@@@2@V?$ allocator@U?$pa ir@$$CBV?$vecto r@HV?
$allocator@H@st d@@@std@@H@std@ @@2@@2@U?$less@ V?$vector@HV?
$allocator@H@st d@@@std@@@2@V?$ allocator@U?$pa ir@$$CBV?$vecto r@HV?
$allocator@H@st d@@@std@@V?$map @V?$vector@HV?$ allocator@H@std @@@std@@HU?
$less@V?$vector @HV?$allocator@ H@std@@@std@@@2 @V?$allocator@U ?$pair@$
$CBV?$vector@HV ?$allocator@H@s td@@@std@@H@std @@@2@@2@@std@@@ 2@@1@@Z)
referenced in function _main
MSVS2005, seems like no concepts. Thx anyway.
Jul 29 '07 #6
ozizus wrote:
>
>a) technically, you want something like:

template < template <class, class class MAP, typename T1, typename T2
> >

Read up on template template parameters (as opposed to typename template
parameters).

I tried the code below, but got error C2065: 'T2' : undeclared
identifier.
You have a typo:
template<templa te<class,classc lass MAP, typename T1, typename t2>
^^
ostream & operator << (ostream & o, MAP<T1,T2& m)
^^
Also: make that a MAP<T1,T2const & m.
^^^^^

Also: don't do this anyway. I already explained why it is a bad idea.

If you want to avoid code duplication, implement a template

template < typename Cont >
std::ostream & write_sequence ( std::ostream & os, Cont const & c )

and use it to implement output for map and multimap.

C++ is huge, is not it, lots of reading up to do.. Thx anyway.
True.
Best

Kai-Uwe Bux
Jul 29 '07 #7
On Jul 29, 3:00 pm, ozizus <o...@ce.yildiz .edu.trwrote:
I overloaded operator << for STL map successfully:
template <typename T1, typename T2ostream & operator << (ostream &
o, map <T1,T2& m)
{
//code
}
the code works like a charm.
Are you sure? In what namespace did you put it? If you put it
in std, then you have undefined behavior. And if you put it in
some other namespace, if you use it in a template, the compiler
will only find it when it is instantiated on a class in the same
namespace (dependant name look-up oblige).

In general, you can't reliably overload operator<< generically
for any of the container types in the standard library (nor can
you overload it for instantiations where only standard types are
involved). In general, of course, you don't want to either.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 29 '07 #8
On Jul 29, 10:34 pm, James Kanze <james.ka...@gm ail.comwrote:
On Jul 29, 3:00 pm, ozizus <o...@ce.yildiz .edu.trwrote:
I overloaded operator << for STL map successfully:
template <typename T1, typename T2ostream & operator << (ostream &
o, map <T1,T2& m)
{
//code
}
the code works like a charm.

Are you sure? In what namespace did you put it?
I put it in no namespace. I use "using namespace std;". This may be
bad practice but it works. Since I overloaded << for other containers,
I can output any object that is a combination of these containers in
one line. Same is true for >too.

eg.
map<vector<int> , set<string>m;
//code
cout<<m;

very powerfull.

Jul 30 '07 #9
On Jul 29, 6:28 pm, ozizus <o...@ce.yildiz .edu.trwrote:
template<typena me MapT>
ostream & operator << (ostream & o, const MapT& m){
typedef typename MapT::Key_type T1;
typedef typename MapT::mapped_ty pe T2;
//code
return o;
};

template<typena me MapTostream & operator << (ostream & o, MapT & m)
{
typedef typename MapT::key_type T1;
typedef typename MapT::mapped_ty pe T2;
..

}

gives the cute one liner below:

error LNK2019: unresolved external symbol "class
std::basic_ostr eam<char,struct std::char_trait s<char & __cdecl
operator<<<clas s std::vector<int ,class std::allocator< int,class
std::map<class std::vector<int ,class std::allocator< int,int,struct
std::less<class std::vector<int ,class std::allocator< int >,class
std::allocator< struct std::pair<class std::vector<int ,class
std::allocator< int const ,int (class
std::basic_ostr eam<char,struct std::char_trait s<char &,class
std::map<class std::vector<int ,class std::allocator< int,class
std::map<class std::vector<int ,class std::allocator< int,int,struct
std::less<class std::vector<int ,class std::allocator< int >,class
std::allocator< struct std::pair<class std::vector<int ,class
std::allocator< int const ,int >,struct std::less<class
std::vector<int ,class std::allocator< int >,class
std::allocator< struct std::pair<class std::vector<int ,class
std::allocator< int const ,class std::map<class
std::vector<int ,class std::allocator< int,int,struct std::less<class
std::vector<int ,class std::allocator< int >,class
std::allocator< struct std::pair<class std::vector<int ,class
std::allocator< int const ,int &)" (??$?6V?$vector @HV?
$allocator@H@st d@@@std@@V?$map @V?$vector@HV?$ allocator@H@std @@@std@@HU?
$less@V?$vector @HV?$allocator@ H@std@@@std@@@2 @V?$allocator@U ?$pair@$
$CBV?$vector@HV ?$allocator@H@s td@@@std@@H@std @@@2@@1@@@YAAAV ?
$basic_ostream@ DU?$char_traits @D@std@@@std@@A AV01@AAV?$map@V ?
$vector@HV?$all ocator@H@std@@@ std@@V?$map@V?$ vector@HV?
$allocator@H@st d@@@std@@HU?$le ss@V?$vector@HV ?
$allocator@H@st d@@@std@@@2@V?$ allocator@U?$pa ir@$$CBV?$vecto r@HV?
$allocator@H@st d@@@std@@H@std@ @@2@@2@U?$less@ V?$vector@HV?
$allocator@H@st d@@@std@@@2@V?$ allocator@U?$pa ir@$$CBV?$vecto r@HV?
$allocator@H@st d@@@std@@V?$map @V?$vector@HV?$ allocator@H@std @@@std@@HU?
$less@V?$vector @HV?$allocator@ H@std@@@std@@@2 @V?$allocator@U ?$pair@$
$CBV?$vector@HV ?$allocator@H@s td@@@std@@H@std @@@2@@2@@std@@@ 2@@1@@Z)
referenced in function _main
That looks like a multiple definition error.try defining some
function :

ostream& out(mytype& m,ostream& os);
MSVS2005, seems like no concepts. Thx anyway.
no surprize it is too new and for ms2005.

MSVC supports templates as arguments but I did not want to suggest
that.As long as (multi)map takes 4 type arguments(last two have
default params) you will need five arguments for such a template:

#define T typename
template< template <T,T,T,TT MapT/*0*/,T Key/*1*/, T Value/*2*/,T
Predicate/*3*/,T Alloc/*4*/>
ostream& (ostream& os, MapT<Key,Value, Predicate,Alloc & m);

on MSVS (multi)map/set derive from 'template<typen ame traitsstruct
_Tree' defined in <xtreeheader.yo u can overload for '_Tree' but that
affects (multi)set as well(potentiall y more classes):

#include<xtree>
using namespace std;
template <typename traits>
ostream& (ostream& os, _Tree<traits& m);

regards,
FM.

Jul 30 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1286
by: Tatu Portin | last post by:
1: #include <iostream> 2: 3: typedef struct { 4: double r; 5: double i; 6: } complex; .. .. .. 24: ostream & operator<< (ostream &str, const complex &a)
10
1642
by: pmatos | last post by:
Hi all, I have the following code: class test { public: test(const std::string *n) : name(n) {} virtual ~test() {} const std::string * getName() { return name; }
3
1320
by: pmatos | last post by:
Hi all, I'm having a problem and for illustration purposes I developed code that shows what the problem is about. However, any comment on the code which is not directly about this issue is surely welcome. I have 3 classes, A is abstract, B and C inherit from A and then I create a vector with B's and C's and print them. Of course, I overload << for each one of them. So it is as follows:
2
1303
by: zeroYouMustNotSpamtype | last post by:
Hi, Describing this problem will be a bit long winded, but please bear with me: I've got three files in my project: permuts.h, permuts.cpp, and braids.cpp (some content from wich will eventually be moved into braids.h). Both cpp files include the h file. Permuts.h contains the Permutation class, for which I needed to overload <<. Doing this in that file caused an error(Duplicate definition I think - it occurs to
3
1644
by: Suresh Tri | last post by:
Hi all, I was trying to overload '<' operator for (varchar,varchar). But in the function which handles the comparision I want to use the previous '<' operator.. but it is going into a recursion. My simplified code looks like : create or replace function orastringcmp (varchar, varchar) returns boolean as 'declare
1
2128
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get the same error. Here is the code of the class template< class T > class Matrix { friend ostream &operator << <>( ostream &, const Matrix< T > & ); friend istream &operator >> <>( istream &, Matrix< T > & ); public: ...
3
1724
by: johnmmcparland | last post by:
Hi all, I know it is possible to overload the operators and < in C++ but how can I do this. Assume I have a class Date with three int members, m_day, m_month and m_year. In the .cpp files I have defined the < and operators; bool Date::operator<(const Date& d) {
8
1722
by: Micko1 | last post by:
hi there, I have an issue when overloading <<. string operator << (room * &currentPlayerLocation); string room::operator << (room * &currentPlayerLocation) {
0
8355
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.