By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

operator<< for a template with a structure

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.