Overloading operator<< to output private class-data

A

Andreas

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::eek:perator<<
[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>::eek:perator<<(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
 
T

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.
ostringstream & operator<<(ostringstream & os, punkt & v)

std::eek:stream& operator<<(std::eek:stream& 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>
 
A

Andreas

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
 
S

scottys0

the best way is .. .

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

don't miss the *friend* keyword
 
T

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::eek:stringstream::eek:perator<<() returns std::eek:stream&.
(std::eek:stream 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::eek:stringstream 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.
 
T

TB

TB skrev:
Andreas skrev:

std::eek:stringstream::eek:perator<<() returns std::eek:stream&.
(std::eek:stream 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::eek:stringstream os;
punkt p;
os << p;

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

I meant:

os << "text" << p;
 
A

Andreas

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top