473,221 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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_traits<char>]((&oss), "String: Scheitel=") <<
scheitel'
/usr/include/g++/bits/ostream.tcc:63: error: candidates are:
***std::basic_ostream<_CharT,*_Traits>&*std::basic _ostream<_CharT,
***_Traits>::operator<<(std::basic_ostream<_CharT,
***_Traits>&(*)(std::basic_ostream<_CharT,*_Traits >&))*[with*_CharT*=*char,
***_Traits*=*std::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 2431
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<<(ostringstream & 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<<(ostringstream & 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::ostringstream::operator<<() returns std::ostream&.
(std::ostream is a common base class for all output stream classes)

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

And it will work with this code:
std::ostringstream 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<<(ostringstream & 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::ostringstream::operator<<() returns std::ostream&.
(std::ostream is a common base class for all output stream classes)

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

And it will work with this code:
std::ostringstream 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::ostringstream::operator<<() returns std::ostream&.
(std::ostream is a common base class for all output stream classes)

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

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

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


I meant:

os << "text" << p;


--
Viele Gre,
Andreas
Mar 5 '06 #7

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

Similar topics

4
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...
3
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
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
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
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...
8
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
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>&...
1
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...
0
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...
10
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.