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 3 1442
"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)
"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)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ernst.stiegler |
last post by:
Hello,
I just want to read a whole line from a console input. What I don't
understand is that I always have to press ENTER twice in order to see
the line I've entered.
Here's my code :
...
|
by: Moritz Steinberger |
last post by:
Hello,
I want to use a tutorial to take first steps. I use MS Visual c++ 6.0 and
Win 98. When I try to compile a "hello world" program using the <iostream.h>
library and cout << "hello world",...
|
by: Christopher |
last post by:
I am trying to get a line from the user which could be anything, I am
expecting either a string followed by a whitespace or just a string by
itself. I have tryed using string::getline,...
|
by: Craig Nicol |
last post by:
Hi everyone,
I'm looking for some help migrating my code from Microsoft VC++ 6.0,
which was not the most conformant compiler I have now discovered. I'm
trying to move to .NET and I've made the...
|
by: Pradyot Dhulipala |
last post by:
Hi,
I am using ns2.26 with the Makefile using C++ compiler version c++
(GCC) 3.2.2 (Mandrake Linux 9.1 3.2.2-3mdk).
When I use iostream.h in a simple program and compile with c++
everything works...
|
by: Charles L |
last post by:
I have read that the inclusion of <fstream.h> makes the inclusion of
<iostream.h> unnecessary.
Is this correct?
Charles L
|
by: humble04 |
last post by:
Hi,
I am compiling a collection of C++ code. Most of them are using the new
format #include <iostream>. I think all of them because I failed at
finding out which header file uses the old format ...
|
by: S. Nurbe |
last post by:
Hi,
probably this is a common problem but I couldn't find yet a proper
solution (and I hope there is one).
I have two relative complex frameworks: one uses only iostream.h the
other one only...
|
by: lars.uffmann |
last post by:
Hello everyone!
I just debugged a pretty huge project, eliminating basically every
memory leak that would occur with the current configuration files, at
least according to the mtrace() tool from...
|
by: Robert Kochem |
last post by:
Hi,
I am relative new to C++ regarding it's functions and libraries. I need to
access files larger than 4GB which is AFAIK not possible with the STL
iostream - at least not if using a 32 bit...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |