468,257 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

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 2200
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dan | last post: by
3 posts views Thread by Robert Wierschke | last post: by
7 posts views Thread by JustSomeGuy | last post: by
10 posts views Thread by pmatos | last post: by
3 posts views Thread by Alex Vinokur | last post: by
8 posts views Thread by jois.de.vivre | last post: by
8 posts views Thread by Ook | last post: by
1 post views Thread by atomik.fungus | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.