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 | | | | re: Overloading operator<< to output private class-data
Andreas skrev:[color=blue]
> Hi,
>
> to output the private data of a class I want to overload the operator<<. The
> Output should be written in a string-variable.
>[/color]
<snip>[color=blue]
>
> ostringstream & operator<<(ostringstream & os, punkt & v)[/color]
std::ostream& operator<<(std::ostream& os, punkt& v)
[color=blue]
> {
> os << "(";
> os.width(4);
> os << v.p.x << ",";
> os.width(4);
> os << v.p.y << ",";
> os.width(4);
> os << v.p.z << ")";
> return os;
> };[/color]
<snip>
--
TB @ SWEDEN | | | | re: Overloading operator<< to output private class-data
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:
[color=blue]
> Andreas skrev:[color=green]
>> Hi,
>>
>> to output the private data of a class I want to overload the operator<<.
>> The Output should be written in a string-variable.
>>[/color]
> <snip>[color=green]
>>
>> ostringstream & operator<<(ostringstream & os, punkt & v)[/color]
>
> std::ostream& operator<<(std::ostream& os, punkt& v)
>[color=green]
>> {
>> os << "(";
>> os.width(4);
>> os << v.p.x << ",";
>> os.width(4);
>> os << v.p.y << ",";
>> os.width(4);
>> os << v.p.z << ")";
>> return os;
>> };[/color]
>
> <snip>
>
>[/color] | | | | re: Overloading operator<< to output private class-data
the best way is .. .
friend std::ostream& operator <<(std::ostream& , const <TYPE>);
don't miss the *friend* keyword | | | | re: Overloading operator<< to output private class-data
Andreas skrev:[color=blue]
> 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).
>[/color]
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.
[color=blue]
> TB wrote:
>[color=green]
>> Andreas skrev:[color=darkred]
>>> Hi,
>>>
>>> to output the private data of a class I want to overload the operator<<.
>>> The Output should be written in a string-variable.
>>>[/color]
>> <snip>[color=darkred]
>>> ostringstream & operator<<(ostringstream & os, punkt & v)[/color]
>> std::ostream& operator<<(std::ostream& os, punkt& v)
>>[color=darkred]
>>> {
>>> os << "(";
>>> os.width(4);
>>> os << v.p.x << ",";
>>> os.width(4);
>>> os << v.p.y << ",";
>>> os.width(4);
>>> os << v.p.z << ")";
>>> return os;
>>> };[/color]
>> <snip>
>>
>>[/color]
>
>[/color]
--
TB @ SWEDEN | | | | re: Overloading operator<< to output private class-data
TB skrev:[color=blue]
> Andreas skrev:[color=green]
>> 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).[/color]
>
> 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;[/color]
I meant:
os << "text" << p;
--
TB @ SWEDEN | | | | re: Overloading operator<< to output private class-data
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:
[color=blue]
> TB skrev:[color=green]
>> Andreas skrev:[color=darkred]
>>> 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).[/color]
>>
>> 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;[/color]
>
> I meant:
>
> os << "text" << p;
>[/color]
--
Viele Grüße,
Andreas |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|