Cascading overloaded cout operator error `No match` when used with other overloaded operators

I have overloaded << in my code as a friend function. Even after using braces while cascading there is still the error

error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = 
std::char_traits<char>]((* & std::cout), ((const char*)"complex conjugate is ")) 
<< c.myComplex::operator~()’

I have a class myComplex

class myComplex
{
    private:
    float real;
    float imaginary;    

    public:
    myComplex();
    myComplex(float a,float b);
    friend std::ostream& operator<<(std::ostream& iso,myComplex& a);
    myComplex operator~() const;
};

The overload for <<, ~and the constructors are as follows,

std::ostream& operator<<(std::ostream& oso,myComplex& a)
{
    oso<<a.real<<" + "<<a.imaginary<<"i"<<std::endl;
return oso;
}

myComplex myComplex::operator~() const
{
    return myComplex(this->real,-1*this->imaginary);
}

myComplex::myComplex(float a,float b)
{
    real = a;
    imaginary = b;
}

I use it in main as follows,

Line 1:    std::cout << "c is " << c << "\n";
Line 2:    std::cout << "a is " << a << "\n";

Line 3: ((std::cout <<"complex conjugate is ")<<(~c))<<"\n";

Line 3:    std::cout <<"complex conjugate is "<<~c<<"\n";

Line 1 and 2 work fine, but Line 3 gives the error. If it can cascade for Line 1 and 2 and since ~c also returns an ostream why does it give an error for that?

Answers


Because your operator~() returns by value, you are feeding a temporary object into your ostream& operator<< here:

std::cout <<"complex conjugate is "<< ~c <<"\n";
//                                    ^^ HERE! Temporary myComplex value.

The operator takes its 2nd argument by non-const reference myComplex&. A non-const reference cannot bind to a temporary. Change it to

std::ostream& operator<<(std::ostream& oso, const myComplex& a);

A temporary object (in this case that returned by operator~()) cannot be bound to a non-const reference. Change argument of operator<<() to be a const reference:

friend std::ostream& operator<<(std::ostream& iso,myComplex const& a);

Use const myComplex & instead of myComplex &:

friend std::ostream& operator<<(std::ostream& iso, const myComplex& a);
                                                   ^^^^^

And change the implementation, of course.


Need Your Help

Vaadin Textarea auto height

java textarea vaadin css

In Vaadin, is it possible to somehow render a textarea that automatically adjusts its height to the content?

How to identify an anonymous function

c# delegates anonymous-function

I have a class that creates a List&lt;Action&lt;int&gt;&gt; and holds on to them until a later time. This class can add and remove delegates from this list. This works well as long as people don't ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.