473,796 Members | 2,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator<< for a template with a structure

Hi all:

I have defined the template

template<class T>
class CGroup {
friend std::ostream & operator<<( std::ostream &lhs, const CGroup
&rhs );
private:
int nSize;
public:
std::deque<T> c;
};

Then, I define a group of people

CGroup<TPerson> myGroup;

where the person structure is defined as

struct TPerson {
int ID;
};

My question is: How do I define operator<< for CGroup in this case? Can
I add athe << operator to TPerson?

Thanks for your help,

Cuneyt

Jul 23 '05 #1
5 3985
cuneyt wrote:
I have defined the template

template<class T>
class CGroup {
friend std::ostream & operator<<( std::ostream &lhs, const CGroup
&rhs );
private:
int nSize;
public:
std::deque<T> c;
};

Then, I define a group of people

CGroup<TPerson> myGroup;

where the person structure is defined as

struct TPerson {
int ID;
};

My question is: How do I define operator<< for CGroup in this case?
What do you mean by "how"? What do you need it to do?
Can
I add athe << operator to TPerson?


There is nothing that says you mustn't. As to whether you _can_,
I don't know. Can you? Just add it and see what happens.

V
Jul 23 '05 #2
Your "friend" declaration is wrong. As CGroup is a template, the stream
operator also has to be a template.

First declare your class as a template thus:

template < typename T > class CGroup;

Then declare the stream function as a template function:

template < typename T >
std::ostream & operator << ( std::ostream& os, const CGroup & rhs );

Now in your class CGroup add in an extra <> thus:

template < typename T >
class CGroup
{
friend std::ostream & operator<< <> ( std::ostream & lhs, const
CGroup & rhs );
// rest of CGroup
};

Now you can define your stream operator too. (Could not do so earlier
as CGroup was incomplete).
You can stream the T. If there is no overload for the T the compiler
will let you know.

Jul 23 '05 #3
Thanks for the replies. I think my main problem is that although I know
how the define the stream operator for classes, I cannot add it to
structures. For example, when I try

struct TPerson {
int ID;
std::ostream& operator<<( std::ostream &lhs, const TPerson &rhs );
};

the compiler gives the error: binary 'operator <<' has too many
parameters

Cuneyt

Jul 23 '05 #4
cuneyt wrote:
Thanks for the replies. I think my main problem is that although I know
how the define the stream operator for classes, I cannot add it to
structures. For example, when I try

struct TPerson {
int ID;
std::ostream& operator<<( std::ostream &lhs, const TPerson &rhs );
};

the compiler gives the error: binary 'operator <<' has too many
parameters


You need to understand that operator<< with a stream as its first argument
cannot be a _member_ of your class. It _could_ be a member of the stream
class, but since you can't modify 'std::ostream', you need to define your
operator<< as a _non-member_, i.e. _outside_ of the struct.

V
Jul 23 '05 #5
This is the declaration of the stream:

template < typename T >
std::ostream & operator << ( std::ostream& os, const CGroup< T > & rhs
);

I missed out the < T >.

It should be declared and defined (later on) outside of the class.
Inside the class you declare it as a friend.

Jul 23 '05 #6

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

Similar topics

4
7174
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): <code:templates.h> #include <string> #include <iostream>
1
1662
by: Jacob Foshee | last post by:
Greetings, Using MS Visual C++ .NET 2003, I have the following: template <class T> class vector3 { // ... friend ostream& operator<<(ostream&, const vector3<T>& ); // ostream operator }:
2
2182
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const Debug_Level ddl;
4
2505
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
1
1408
by: ruchirp | last post by:
Hi, I've been trying to overload << for a function similar to boost lambda library. I want to make for_each(v.begin(), b.end(), cout << _1 << endl); to work, however, i'm getting a lot of problems in making this happen.. The following is my current code > #include <string> #include <iostream> using namespace std;
4
1787
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right? The code is as follows... // main class: template < class Individual,
2
1932
by: Joe Hesse | last post by:
Hi, I have a template class and I want to define an operator << as a friend function. For each instantiation of the class I want a corresponding instantiation of operator <<. The following example fails to compile with g++ version 4.1.2. I would appreciate it if you could help me fix it or point me to a suitable reference. Thank you,
2
2845
by: soy.hohe | last post by:
Hi all I have a class StreamLogger which implements operator << in this way: template <class TStreamLogger& operator<<(const T& t) { <print the stuff using fstream, etc.> return *this; }
1
1420
by: Stuart Golodetz | last post by:
Hi guys, I'm trying to making an instance of a templated operator<< for a templated class a friend of that class (see below), to allow it to access the class internals for output purposes. #include <iostream> template <typename T> class TC
0
9527
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
10453
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
10223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7546
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
6785
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
5441
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...
1
4115
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
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.