I want to use std::ostream
like this:
int main()
{
std::ostream os;
os << "something ..." << std::endl;
return 0;
}
There's an error said that the ostream constructor is protected:
error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits]’ is protected.
But I remember operator<<
could be overloaded like this:
// In a class.
friend std::ostream & operator<<(std::ostream& out, const String & s) {
out << s.m_s;
return out;
}
Any advice on why my code doesn't work?
#include <ostream>
preprocessor directive? I also don't think there's a parameterlessostream
constructor - see here.ostream
s should be wrapped around a stream buffer - did you mean to use anfstream
or similar instead? – Dishonesty#include <ostream>
. Pre C++11, just includingiostream
wasn't always enough to includeostream
(although I can't find the SO post that explains this now). Yes - you can only create anostream
object if you pass in astreambuf
object as a parameter. Then theostream
will output to that buffer. See this post for a simple explanation. – Dishonestyostream
object, it's far more likely that you want astringstream
orfstream
. – Dishonesty