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
    float real;
    float imaginary;    

    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?


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

RTE should not wrap a <p> tag around an <img> tag

configuration config typo3 typoscript rte

I work with the RTE and it makes me angry that the RTE does wrap &lt;p&gt; tags around an &lt;img&gt; tag. What do I have to do that the RTE does not wrap &lt;p&gt; tags around the &lt;img&gt; tag?

Mock IRavenQueryable with a Where() expression appended

c# unit-testing mocking moq ravendb

I'm trying to do some basic proof of concept type code for a new mvc3 project. We are using Moq with RavenDB.

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.