iostream problems

P

Pushkar Pradhan

I'm overloading the << operator to o/p a list of structures like this:
typedef struct point {
int x;
int y;
} PARTICLE;
so that I can print out the list using:
cout << "hull: " << hull << endl;

This is how I overloaded the << operator before main():
//--- Overload output operator for list<T>
template <typename T>
ostream & operator<<(ostream & out, const list<T> & l) {
for(list<T>::iterator i = l.begin(); i != l.end(); i++)
//out << *i.x << *i.y << " ";
return out;
}

However I get this compile error:
main.cpp: In function `class ostream & operator <<<PARTICLE>(ostream &,
const list<point,allocator<point> > &)':
main.cpp:66: instantiated from here
main.cpp:18: conversion from `_List_iterator<point,const point &,const
point *>' to non-scalar type `_List_iterator<point,point &,point *>'
requested
main.cpp:66: instantiated from here
main.cpp:18: no match for `_List_iterator<point,point &,point *> & !=
_List_iterator<point,const point &,const point *>'
/usr/local/gnu/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_list.h:70:
candidates are: bool _List_iterator<point,point &,point *>::eek:perator
!=(const _List_iterator<point,point &,point *> &) const

Can anyone tell me what more do I need to do?
Pushkar Pradhan
 
J

John Carson

Pushkar Pradhan said:
I'm overloading the << operator to o/p a list of structures like this:
typedef struct point {
int x;
int y;
} PARTICLE;
so that I can print out the list using:
cout << "hull: " << hull << endl;

This is how I overloaded the << operator before main():
//--- Overload output operator for list<T>
template <typename T>
ostream & operator<<(ostream & out, const list<T> & l) {
for(list<T>::iterator i = l.begin(); i != l.end(); i++)
//out << *i.x << *i.y << " ";
return out;
}

The following code works for me:

struct PARTICLE
{
int x;
int y;
};

template <class T>
ostream & operator<<(ostream & out, const list<T> & l)
{
for(list<T>::const_iterator i = l.begin(); i != l.end(); ++i)
out << i->x << i->y << " ";
return out;
}

int main()
{
list<PARTICLE> ls;
PARTICLE p1 = {0,1};
PARTICLE p2 = {2,3};
ls.push_back(p1);
ls.push_back(p2);
cout << ls << endl;
return 0;
}

Note the following:

1. I have dropped the typedef stuff in the declaration of PARTICLE. It is
unnecessary --- a hangover from C.

2. Since the list passed as an argument to the operator is const, you need
to use const_iterator.

3. Rather than

out << *i.x << *i.y << " ";

it should be

out << i->x << i->y << " ";

The selection operator . has a higher precedence than the dereferencing
operator *, so your original code has the effect of

out << *(i.x) << *(i.y) << " ";

which you don't want. If you really want to retain the * and . then you can
do it like this:

out << (*i).x << (*i).y << " ";
 
J

John Carson

John Carson said:
The following code works for me:

struct PARTICLE
{
int x;
int y;
};

template <class T>
ostream & operator<<(ostream & out, const list<T> & l)
{
for(list<T>::const_iterator i = l.begin(); i != l.end(); ++i)
out << i->x << i->y << " ";
return out;
}

int main()
{
list<PARTICLE> ls;
PARTICLE p1 = {0,1};
PARTICLE p2 = {2,3};
ls.push_back(p1);
ls.push_back(p2);
cout << ls << endl;
return 0;
}

Note the following:

1. I have dropped the typedef stuff in the declaration of PARTICLE.
It is unnecessary --- a hangover from C.

2. Since the list passed as an argument to the operator is const, you
need to use const_iterator.

3. Rather than

out << *i.x << *i.y << " ";

it should be

out << i->x << i->y << " ";

The selection operator . has a higher precedence than the
dereferencing operator *, so your original code has the effect of

out << *(i.x) << *(i.y) << " ";

which you don't want. If you really want to retain the * and . then
you can do it like this:

out << (*i).x << (*i).y << " ";


Incidentally, since your overload only seems to make sense for lists of
PARTICLES, I don't know why you are using templates. Why not just:

ostream & operator<<(ostream & out, const list<PARTICLE> & l)
{
for(list<PARTICLE>::const_iterator i = l.begin(); i != l.end(); ++i)
out << i->x << i->y << " ";
return out;
}
 
T

Tommi =?ISO-8859-15?Q?M=E4kitalo?=

Pushkar Pradhan wrote:
use (*i).x or i->x. *i.x is interpreted as *(i.x), so you try to dereference
x of PARTICLE, which is a int.

Tommi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top