By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,728 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

ostream and ofstream overloading

P: n/a
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::ofstream& operator<<( std::ofstream &fos, const Foo &theClass);
std::ostream& operator<<( std::ostream &fos, const Foo &theClass);
The problem is that I keep getting this error with G++

choosing 'std::basic_ostream<_CharT, _Traits>& std::operator<<
(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::ofstream& operator<<(
std::ofstream &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.


Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Chase Bradford" <ch******@hotmail.com> wrote in message
news:bp***********@news.fsr.net...
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::ofstream& operator<<( std::ofstream &fos, const Foo &theClass);
std::ostream& operator<<( std::ostream &fos, const Foo &theClass); I wouldn't do this, because it will not work as expected when
chaining output calls with << :
void test( std::ofstream& 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::ostream
}
The problem is that I keep getting this error with G++

choosing 'std::basic_ostream<_CharT, _Traits>& std::operator<<
(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::ofstream& operator<<(
std::ofstream &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
--
http://ivan.vecerina.com
Jul 19 '05 #2

P: n/a
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" <pl*****************@ivan.vecerina.com> wrote in message
news:bp**********@newshispeed.ch...
"Chase Bradford" <ch******@hotmail.com> wrote in message
news:bp***********@news.fsr.net...
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::ofstream& operator<<( std::ofstream &fos, const Foo &theClass);
std::ostream& operator<<( std::ostream &fos, const Foo &theClass);

I wouldn't do this, because it will not work as expected when
chaining output calls with << :
void test( std::ofstream& 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::ostream
}
The problem is that I keep getting this error with G++

choosing 'std::basic_ostream<_CharT, _Traits>& std::operator<<
(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::ofstream& operator<<(
std::ofstream &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
--
http://ivan.vecerina.com

Jul 19 '05 #3

P: n/a
On Sun, 16 Nov 2003 13:36:15 -0800, "Chase Bradford"
<ch******@hotmail.com> wrote:
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.


You could add formatting manipulators for your class. Check out this
article:

http://www.cuj.com/documents/s=7998/cujcexp1902austern/

Tom
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.