473,765 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading operator<< to output private class-data

Hi,

to output the private data of a class I want to overload the operator<<. The
Output should be written in a string-variable.

To do this i have written the attached code:

But the program-compilation aborts with the following error message:

StreamKoord.cpp : In function `int main()':
StreamKoord.cpp :44: error: no match for 'operator<<' in 'std::operator< <
[with _Traits = std::char_trait s<char>]((&oss), "String: Scheitel=") <<
scheitel'
/usr/include/g++/bits/ostream.tcc:63: error: candidates are:
***std::basic_o stream<_CharT,* _Traits>&*std:: basic_ostream<_ CharT,
***_Traits>::op erator<<(std::b asic_ostream<_C harT,
***_Traits>&(*) (std::basic_ost ream<_CharT,*_T raits>&))*[with*_CharT*=*c har,
***_Traits*=*st d::char_traits< char>]
*...

A lot of further candidates are listed. But my additional method wasn't
listed.

What is wrong in my code and how can i solve this problem?

Many thanks in advance,
Andreas

PS: gcc: 3.3.4 / linux 2.6.8-24.11-smp
Mar 4 '06 #1
6 2446
TB
Andreas skrev:
Hi,

to output the private data of a class I want to overload the operator<<. The
Output should be written in a string-variable.
<snip>
ostringstream & operator<<(ostr ingstream & os, punkt & v)
std::ostream& operator<<(std: :ostream& os, punkt& v)
{
os << "(";
os.width(4);
os << v.p.x << ",";
os.width(4);
os << v.p.y << ",";
os.width(4);
os << v.p.z << ")";
return os;
};


<snip>
--
TB @ SWEDEN
Mar 4 '06 #2
Hi TB,

many thanks for your help!

Whats the rule behind this? I thought that it is necessary to use the same
Output-type for operator<< as defined for oss (like my example).

Sincerely,
Andreas

TB wrote:
Andreas skrev:
Hi,

to output the private data of a class I want to overload the operator<<.
The Output should be written in a string-variable.

<snip>

ostringstream & operator<<(ostr ingstream & os, punkt & v)


std::ostream& operator<<(std: :ostream& os, punkt& v)
{
os << "(";
os.width(4);
os << v.p.x << ",";
os.width(4);
os << v.p.y << ",";
os.width(4);
os << v.p.z << ")";
return os;
};


<snip>

Mar 4 '06 #3
the best way is .. .

friend std::ostream& operator <<(std::ostream & , const <TYPE>);

don't miss the *friend* keyword

Mar 4 '06 #4
TB
Andreas skrev:
Hi TB,

many thanks for your help!

Whats the rule behind this? I thought that it is necessary to use the same
Output-type for operator<< as defined for oss (like my example).

std::ostringstr eam::operator<< () returns std::ostream&.
(std::ostream is a common base class for all output stream classes)

Your original declaration was:
ostringstream & operator<<(ostr ingstream & os, punkt & v);

And it will work with this code:
std::ostringstr eam os;
punkt p;
os << p;

But not here:
os << p << p;

If you ask why, reread this reply from the top and look at the error
message your compiler produces.
TB wrote:
Andreas skrev:
Hi,

to output the private data of a class I want to overload the operator<<.
The Output should be written in a string-variable.

<snip>
ostringstream & operator<<(ostr ingstream & os, punkt & v)

std::ostream& operator<<(std: :ostream& os, punkt& v)
{
os << "(";
os.width(4);
os << v.p.x << ",";
os.width(4);
os << v.p.y << ",";
os.width(4);
os << v.p.z << ")";
return os;
};

<snip>



--
TB @ SWEDEN
Mar 4 '06 #5
TB
TB skrev:
Andreas skrev:
Hi TB,

many thanks for your help!

Whats the rule behind this? I thought that it is necessary to use the
same
Output-type for operator<< as defined for oss (like my example).


std::ostringstr eam::operator<< () returns std::ostream&.
(std::ostream is a common base class for all output stream classes)

Your original declaration was:
ostringstream & operator<<(ostr ingstream & os, punkt & v);

And it will work with this code:
std::ostringstr eam os;
punkt p;
os << p;

But not here:
os << p << p;


I meant:

os << "text" << p;

--
TB @ SWEDEN
Mar 4 '06 #6
Hi,

thanks!

Another small step for the mankind, but a big step for me.

I believe now its clear why it is necassary to use the baseclass to overload
operators!

Sincererly,
Andreas

TB wrote:
TB skrev:
Andreas skrev:
Hi TB,

many thanks for your help!

Whats the rule behind this? I thought that it is necessary to use the
same
Output-type for operator<< as defined for oss (like my example).


std::ostringstr eam::operator<< () returns std::ostream&.
(std::ostream is a common base class for all output stream classes)

Your original declaration was:
ostringstream & operator<<(ostr ingstream & os, punkt & v);

And it will work with this code:
std::ostringstr eam os;
punkt p;
os << p;

But not here:
os << p << p;


I meant:

os << "text" << p;


--
Viele Grüße,
Andreas
Mar 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2227
by: Dan | last post by:
Hi, I would just like to know if the istream operator takes only one parammeter(object) at a time (like z) ? istream operator>>(istream& in, Shape &z) Cause I keep getting error concerning the amount my operator has for bother cin , cout operator<< and >> thanks Dan
3
2034
by: Robert Wierschke | last post by:
Hi I want to overload the operator<< for a class Vector. class Vector { double x; double y; double z;
7
1650
by: JustSomeGuy | last post by:
I have a double that I want to output as: (depending on its value) 1000 Bytes 1.6 Kilobyte 2.5 Megabytes ..5 Terabytes can I do this with... ostream & operator<<(ostream & o, double n)
10
1655
by: pmatos | last post by:
Hi all, I have the following code: class test { public: test(const std::string *n) : name(n) {} virtual ~test() {} const std::string * getName() { return name; }
3
2049
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look fine? // --------- foo.cpp --------- #include <iostream> using namespace std;
8
8374
by: jois.de.vivre | last post by:
Hi, I'm having some trouble overloading the << operator. I have the following, very simple code: #include <iostream> using namespace std; class test { private: int val;
8
2026
by: Ook | last post by:
This is my code in it's entireity: #include <iostream> using namespace std; template <typename T> class SortedList { public: friend ostream& operator<< (ostream& os, const SortedList<T>& lst );
1
2134
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get the same error. Here is the code of the class template< class T > class Matrix { friend ostream &operator << <>( ostream &, const Matrix< T > & ); friend istream &operator >> <>( istream &, Matrix< T > & ); public: ...
0
1326
by: Adrian | last post by:
Hi All, I am trying to create an output operator that doesnt rely on the stream type and is also polymorphic. Is there a way to combine these 2 methods so that the polymorphic output operator has template arguments. I have tried a few ways and my compiler just wont let me. Method 1: Doesn't rely on the char type of a stream --------------------------------------------------- class A
10
1822
by: ozizus | last post by:
I overloaded operator << for STL map successfully: template <typename T1, typename T2ostream & operator << (ostream & o, map <T1,T2& m) { //code } the code works like a charm. Now, I want the same functionality for multimap. Since their interface is same for the problem at hand, I
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.