Deriving From ofstream - endl not working
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
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?
As to your problem, could be it needs a namespace operator? E.g. std::endl?
Firstly, as already mentioned, do not inherit from
STL IO manipulators are global functions, and
In your case, the definitions would look something like so:
[ 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]
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]
Thanks for the replies - I dont quite understand why the ofstream is not meant to be derived from. Can anyone back this up with a reason?
Thanks Aface
Thanks Aface
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]
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement