cout
is not of type ofstream
, so you cannot bind an ofstream
reference to it. output_stream
should be type ostream&
instead, which will allow it to refer to either cout
and a file stream, since ofstream
is a subclass of ostream
.
Also, in the case that the user provides a filename, you still need something for the reference to refer to, you can't just use it as is. I recommend that you store an actual ofstream
object, (or a unique_ptr<ofstream>
), and make output_stream
refer to it. Make sure you declare the ofstream
object before the ostream
reference in your class definition, otherwise you will have undefined behavior when you try to bind the reference in the initialization list. Or you can make it a pointer, instead of a reference, and assign it in the body of the constructor.