0

I have a class complex and I would like to operator << could print its private variables.

class complex
{
   double re, im;

   public:
   friend ostream operator <<(ostream &out); // What's wrong?
};

Is it possible?

  • 1
    `operator<<` is a binary operator, and you tried to declare it with only one operand. You can't change arity of C++ operators when overloading. You must declare it correctly as a binary operator, for example: `friend ostream operator <<(ostream &out, const complex &what_to_output);` – Ben Voigt Oct 24 '15 at 19:08
  • 1
    Your title says "without overloading". Any declaration of `operator <<` is overloading. – Keith Thompson Oct 24 '15 at 19:31

3 Answers3

3

You have to pass two arguments into operator <<() (a reference to the stream object and one to the object you want to stream) and you generally always want to return a reference to the stream passed in so you can pass the output to another invocation of operator<<(). So you need something like:

 friend ostream& operator <<(ostream &out, const complex& rhs);
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • You also need to return by reference. – Alan Stokes Oct 24 '15 at 19:12
  • Strictly speaking, you don't *have to* return a reference. You don't have to return anything at all. But if you're going to return the ostream object which was passed in (which you should, in order to meet the justified expectations that stream operations can be chained), it needs to be by reference, because `ostream` is non-copyable. – Benjamin Lindley Oct 24 '15 at 19:27
  • @BenjaminLindley Yes, of course. But when would ever *not* return a reference to the stream object passed in? – Paul Evans Oct 24 '15 at 19:30
  • I'm not saying you would. I'm just saying that the phrasing you used (*"must return a reference to the stream passed in"*) implies that it is a requirement of the language, rather than being a convention. – Benjamin Lindley Oct 24 '15 at 19:31
  • @BenjaminLindley Ah, I see your point. Edited answer accordingly. – Paul Evans Oct 24 '15 at 19:36
1

Object to be outputed must be passed as parameter:

friend ostream& operator <<(ostream &out, const complex& obj);

Then, you have to implement the function, possible like this:

ostream& operator <<(ostream &out, const complex& obj)
{
    out << obj.re << ";" << obj.im;
    return out;
}
jpo38
  • 20,821
  • 10
  • 70
  • 151
0

yes it is possible but you make a mistake in operator parameter list, there is two parameter not one, one for ostream which the compiler automatically recognize it and in here it must be ostream, the second one will be the class type you will use after cout<<classType in here is Complex, by the way look at this cout<<Complex cout is first parameter, so then you should use these code it will works.

#include <ostream>
using std::ostream;

class Complex
{
public:
    friend ostream &operator <<(ostream &out, Complex &cmplx)
    {
        out << cmplx.im << "\t" << cmplx.re;

        return out;
    }

private:
    double re, im;
};

int main()
{
    Complex complex;

    cout<<complex;

    return 0;
}

it will print the value of re and im.