2
votes

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?

1
Do you have an #include <ostream> preprocessor directive? I also don't think there's a parameterless ostream constructor - see here. ostreams should be wrapped around a stream buffer - did you mean to use an fstream or similar instead? - hnefatl
Yes, I have included <iostream>. I want to use std::ostream like the case in overloaded operator<<(). Therefore the constructor should have a streambuffer as its parameters ? - Jaden
No, I meant #include <ostream>. Pre C++11, just including iostream wasn't always enough to include ostream (although I can't find the SO post that explains this now). Yes - you can only create an ostream object if you pass in a streambuf object as a parameter. Then the ostream will output to that buffer. See this post for a simple explanation. - hnefatl
What are you actually trying to do? Update your question with your end goal. It's unlikely that you actually want to use a raw ostream object, it's far more likely that you want a stringstream or fstream. - hnefatl
@Jaden, I've renewed my answer. I hope that it may helps. - Akira

1 Answers

5
votes

The std::ostream, the std::istream or the std::iostream are base classes of stream types (e.g. std::stringstream, std::fstream, etc.) in the Standard Library. These classes are protected against instantiation, you can instantiate their derived classes only. The error message

error: 'std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits]' is protected

tells you the same.

Your second example is valid because you can use references to the base class of derived classes. In this case no constructor is called, a reference only refers to an existing object. Here is an example how can use std::ostream& to the std::cout:

#include <iostream>

int main() {
    std::ostream& os = std::cout;
    os << "something ..." << std::endl;
}

The reason behind using std::ostream& in overload of operator<< is that you may don't want to overload the the mentioned operator for all individual stream types, but only for the common base class of them which has the << functionality.