ostream and ofstream overloading

C

Chase Bradford

Hey all
I have a class Foo, and I'm trying to overload the << operator for both
the ostream and ofstream for it. This way I should have two seperate
formats for output, one for files and another for the screen. Right now the
two declarations are:


std::eek:fstream& operator<<( std::eek:fstream &fos, const Foo &theClass);
std::eek:stream& operator<<( std::eek:stream &fos, const Foo &theClass);


The problem is that I keep getting this error with G++

choosing 'std::basic_ostream<_CharT, _Traits>& std::eek:perator<<
(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT,
_Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>,
_Alloc = std::allocator<char>]' over 'std::eek:fstream& operator<<(
std::eek:fstream &fos, const Foo &theClass)'

because worst conversion for the former is better than the worst conversion
for the latter


Googling for this error didn't turn up anything productive, so I was
wondering if it is possible to even do this?


Thanks.
 
I

Ivan Vecerina

Chase Bradford said:
I have a class Foo, and I'm trying to overload the << operator for both
the ostream and ofstream for it. This way I should have two seperate
formats for output, one for files and another for the screen. Right now the
two declarations are:


std::eek:fstream& operator<<( std::eek:fstream &fos, const Foo &theClass);
std::eek:stream& operator<<( std::eek:stream &fos, const Foo &theClass);
I wouldn't do this, because it will not work as expected when
chaining output calls with << :
void test( std::eek:fstream& fos , const Foo& theClass )
{
fos << theClass; // will call the 2nd operator
fos << "The class is: " << theClass; // will call the 1st one
// because the first << operator will return an std::eek:stream
}
The problem is that I keep getting this error with G++

choosing 'std::basic_ostream<_CharT, _Traits>& std::eek:perator<<
(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT,
_Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>,
_Alloc = std::allocator<char>]' over 'std::eek:fstream& operator<<(
std::eek:fstream &fos, const Foo &theClass)'
[ NB: you should provide a complete sample if possible ]
Does the Foo class happen to provide an implicit conversion,
eventually to std::string ? [now this can only be a guess]

I'm afraid you are abusing overloading and implicit conversions.
They can be useful techniques, but are also very error prone.
It would be safer to:
- Use dedicated functions instead of already overloaded operators
when wanting to provide non-standard behavior.
- Implicit conversions should be used sparingly -- named member
functions shall be preferred in most cases.


I hope this helps,
Ivan
 
C

Chase Bradford

Thanks Ivan,

I was worried that might be the case so I had already implemented it with
the Push2File dedicated function. I was just hoping there was a way to do
this using the << operator with the particular streams.

Ivan Vecerina said:
Chase Bradford said:
I have a class Foo, and I'm trying to overload the << operator for both
the ostream and ofstream for it. This way I should have two seperate
formats for output, one for files and another for the screen. Right now the
two declarations are:


std::eek:fstream& operator<<( std::eek:fstream &fos, const Foo &theClass);
std::eek:stream& operator<<( std::eek:stream &fos, const Foo &theClass);
I wouldn't do this, because it will not work as expected when
chaining output calls with << :
void test( std::eek:fstream& fos , const Foo& theClass )
{
fos << theClass; // will call the 2nd operator
fos << "The class is: " << theClass; // will call the 1st one
// because the first << operator will return an std::eek:stream
}
The problem is that I keep getting this error with G++

choosing 'std::basic_ostream<_CharT, _Traits>& std::eek:perator<<
(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT,
_Traits, _Alloc>&) [with _CharT = char, _Traits =
std::char_traits said:
_Alloc = std::allocator<char>]' over 'std::eek:fstream& operator<<(
std::eek:fstream &fos, const Foo &theClass)'
[ NB: you should provide a complete sample if possible ]
Does the Foo class happen to provide an implicit conversion,
eventually to std::string ? [now this can only be a guess]

I'm afraid you are abusing overloading and implicit conversions.
They can be useful techniques, but are also very error prone.
It would be safer to:
- Use dedicated functions instead of already overloaded operators
when wanting to provide non-standard behavior.
- Implicit conversions should be used sparingly -- named member
functions shall be preferred in most cases.


I hope this helps,
Ivan
 

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,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top