I want to make a behavior like std::cout has:
int a = 10, b = 15, c = 7; MyBaseClass << "a = " << a << ", b = " << b << std::endl; I try to implement some things which I've just read but it doesn't work for me. I want to implement operator in one class which I call MyBaseClass. I tried this:
class MyBaseClass { private: std::ostream someOut; public: // My first try: std::ostream &operator<< ( std::ostream &out, const std::string &message ) { } // The second try: std::ostream &operator<< ( const std::string &message ) { someOut << message << std::endl; return someOut; } void writeMyOut() { std::cout << someOut.str() }; }; When I compile this I get: "Call to implicity-deleted default constructor of 'MyBaseClass'" - what do I need to do to fix it?
OS X, Xcode, clang compiler, all is up-to-date.
MyBaseClasshas no constructors, but you need one to initialize thesomeOutmember. (I'm not 100% sure that's the problem, but if you provide a constructor forMyBaseClass, I think you'll get a little further. Specifically, I think you'll get far enough to try to write on the uninitializedstd::ostreamcalledsomeOut.)Field of type 'std::ostream' (aka 'basic_ostream<char>') has protected default constructoroperator<<to stream a user-defined type (per your duplicate candidate), but about making a type to which other types can be streamed. It's probably a duplicate too, but harder to find.std::ostream. I'm not sure you can create a plainostream-- you generally either create anofstreamor anostringstream(maybe now called anostrstream? I forget.) Try replacingstd::ostreamwithstd::ostringstream.