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

iostream problems

P: n/a
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 *>::operator
!=(const _List_iterator<point,point &,point *> &) const

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

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote in message
news:3F**************@gri.msstate.edu
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 << " ";
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 22 '05 #2

P: n/a
"John Carson" <do***********@datafast.net.au> wrote in message
news:3f********@usenet.per.paradox.net.au
"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote in message
news:3F**************@gri.msstate.edu
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 << " ";
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

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;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3

P: n/a
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
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 *>::operator
!=(const _List_iterator<point,point &,point *> &) const

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


--
Tommi Mäkitalo
Dr. Eckhardt + Partner GmbH
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.