2

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.

6
  • Possible duplicate C++ Beginner - 'friend' functions and << operator overloading: What is the proper way to overload an operator for a class? Commented Apr 12, 2016 at 4:56
  • I think your second try should work, more or less. The compile error is probably because MyBaseClass has no constructors, but you need one to initialize thesomeOut member. (I'm not 100% sure that's the problem, but if you provide a constructor for MyBaseClass, I think you'll get a little further. Specifically, I think you'll get far enough to try to write on the uninitialized std::ostream called someOut.) Commented Apr 12, 2016 at 4:58
  • @DaveM. - if I add an empty constructor I get: Field of type 'std::ostream' (aka 'basic_ostream<char>') has protected default constructor Commented Apr 12, 2016 at 5:01
  • @Joel: this question is not about creating an operator<< 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. Commented Apr 12, 2016 at 5:01
  • The problem is, you're not initializing the std::ostream. I'm not sure you can create a plain ostream -- you generally either create an ofstream or an ostringstream (maybe now called an ostrstream? I forget.) Try replacing std::ostream with std::ostringstream. Commented Apr 12, 2016 at 5:06

1 Answer 1

3

You're trying to output a variety of value types into the MyBaseClass object, so need to support the same set. I've also changed someOut to be a std::ostringstream, which is capable of accumulating the output. You might equally have wanted it to be a std::ostream& to a caller-provided stream passed to the constructor....

class MyBaseClass { private: std::ostringstream someOut; public: ...other functions... // The second try: template <typename T> MyBaseClass& operator<< ( const T& x ) { someOut << x; return *this; } void writeMyOut() const { std::cout << someOut.str() }; }; 
Sign up to request clarification or add additional context in comments.

4 Comments

In this case I get: Call to implicitly-deleted default constructor of 'MyBaseClass'
@JavaRunner: have you provided any MyBaseClass constructors yourself (perhaps at DaveM's prompting)? If so, remove them. With std::ostream& someOut; you'd need MyBaseClass(std::ostream& os) : someOut(os) { }, but you don't need to specify any constructor for std::ostringstream.
Thanks, your solution works like a charm! It was my fault.
@JavaRunner: sure, no worries. Hope you enjoy your C++ coding. Cheers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.