Archived

This topic is now archived and is closed to further replies.

Deriving From ofstream - endl not working

This topic is 5170 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I have a question regarding the deriving of ofstream. I have a debug file class which is basically derived from the ofstream with a few differences (ie creates file & header on construction etc). I also overload the stream operator to flush every time anything is streamed to file (dont want to lose file updates due to crashes) as a templated member function. Everything worked fine until i streamed an endl character, upon which i received a compiler error C2679: binary ''<<'' : no operator defined which takes a right-hand operand of type ''''. I am guessing the template isnt coping with the fact endl doesnt return characters because it''s last action is to flush the file? Also, is this just plain ugly? i like the syntax of having a global debug which i can stream onto instead of having to sprintf my args. code as below class CDebug: public std::ofstream { public: //overload the constructor / destructor of a file stream CDebug(); virtual ~CDebug(); // overload the stream operator template CDebug& operator <<(T rhs) { *((std::ofstream*)this) << rhs; this->flush(); return *this;} }; Thanks Aface

Share this post


Link to post
Share on other sites
I wouldn''t really recommend inheriting from an STL-class. I''d use a stream as data member in the debug class. This approach lets me stream debug output to debug console and log-file at the same time for example.

As to your problem, could be it needs a namespace operator? E.g. std::endl?

Share this post


Link to post
Share on other sites
Firstly, as already mentioned, do not inherit from std::ofstream. It is not meant for deriving from.

STL IO manipulators are global functions, and std::ostream has an overloaded operator<< that takes a function pointer for the form they take. The definition just calls the function addressed by the function pointer passed to it, passing *this.

In your case, the definitions would look something like so:

CDebug& operator<<( std::ostream& (*pfunc)(std::ostream&) ) // function pointer

{
pfunc(this->os); // `os' being your std::ofstream object member

return *this;
}

CDebug& operator<<( std::ios& (*pfunc)(std::ios&) ) // function pointer

{
pfunc(this->os); // `os' being your std::ofstream object member

return *this;
}

CDebug& operator<<( std::ios_base& (*pfunc)(std::ios_base&) ) // function pointer

{
pfunc(this->os); // `os' being your std::ofstream object member

return *this;
}


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on October 15, 2003 1:23:41 PM]

Share this post


Link to post
Share on other sites
Because you''re meant to derive from streambuffers instead. Technically you can still derive from stream classes but only as a convenience method to save you from having to initialise the specific type of streambuffer you need each time.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites