Connecting Tech Pros Worldwide Forums | Help | Site Map

ostream and ofstream overloading

Chase Bradford
Guest
 
Posts: n/a
#1: Jul 19 '05
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.





Ivan Vecerina
Guest
 
Posts: n/a
#2: Jul 19 '05

re: ostream and ofstream overloading


"Chase Bradford" <chilip00@hotmail.com> wrote in message
news:bp6krf$1kf3$1@news.fsr.net...[color=blue]
> 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[/color]
the[color=blue]
> two declarations are:
>
>
> std::ofstream& operator<<( std::ofstream &fos, const Foo &theClass);
> std::ostream& operator<<( std::ostream &fos, const Foo &theClass);[/color]
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
}
[color=blue]
> 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)'[/color]
[ 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


Chase Bradford
Guest
 
Posts: n/a
#3: Jul 19 '05

re: ostream and ofstream overloading


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" <please_use_web_form@ivan.vecerina.com> wrote in message
news:bp7mnc$jlo$1@newshispeed.ch...[color=blue]
> "Chase Bradford" <chilip00@hotmail.com> wrote in message
> news:bp6krf$1kf3$1@news.fsr.net...[color=green]
> > I have a class Foo, and I'm trying to overload the << operator for[/color][/color]
both[color=blue][color=green]
> > 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[/color]
> the[color=green]
> > two declarations are:
> >
> >
> > std::ofstream& operator<<( std::ofstream &fos, const Foo &theClass);
> > std::ostream& operator<<( std::ostream &fos, const Foo &theClass);[/color]
> 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
> }
>[color=green]
> > 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 =[/color][/color]
std::char_traits<char>,[color=blue][color=green]
> > _Alloc = std::allocator<char>]' over 'std::ofstream& operator<<(
> > std::ofstream &fos, const Foo &theClass)'[/color]
> [ 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
>
>[/color]


tom_usenet
Guest
 
Posts: n/a
#4: Jul 19 '05

re: ostream and ofstream overloading


On Sun, 16 Nov 2003 13:36:15 -0800, "Chase Bradford"
<chilip00@hotmail.com> wrote:
[color=blue]
>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.[/color]

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

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

Tom
Closed Thread