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]