473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3971
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
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): ...
1
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
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...
4
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
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...
4
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?...
2
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...
2
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
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. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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...

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.