title: Temporary objects, c++
body: <p>I'm writing a class for complex numbers and I used operator overloading for <code>+operator</code> and <code>&lt;&lt;operator</code></p>
  <p>I noticed that when I try to print:</p>
  <pre><code>Complex z1(5);
  Complex z2(0,1);
  Complex z6 = z1+z2;
  cout&lt;&lt;z6;
  </code></pre>
  <p>It works fine. But when I try:</p>
  <pre><code>Complex z1(5);
  Complex z2(0,1);
  cout&lt;&lt;(z1+z2);
  </code></pre>
  <p>I get an error:</p>
  <pre><code>error: no match for 'operator&lt;&lt;' (operand types are 'std::ostream' {aka 'std::basic_ostream&lt;char&gt;'} and 'Complex')|
  </code></pre>
  <p>Why is this happaning? why <code>cout&lt;&lt;1+3;</code> is legal but <code>cout&lt;&lt;z1+z2;</code> is not ?</p>
  <p>Here is my decleration of the operator overloading:</p>
  <pre><code>    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, Complex&amp; z);
      Complex operator+(const Complex&amp; z) const;
  </code></pre>
  <p>And the implementation:</p>
  <pre><code>Complex Complex::operator+(const Complex&amp; z) const{
  
      Complex addition;
      addition.imaginary = this-&gt;imaginary + z.imaginary;
      addition.real = this-&gt;real + z.real;
      return addition;
  }
  
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, Complex&amp; z) {
      if(z.real!=0){
          if (z.imaginary&gt;0 &amp;&amp; z.imaginary !=1)
              out&lt;&lt;z.real&lt;&lt;&quot; + &quot;&lt;&lt;z.imaginary&lt;&lt;&quot;i&quot;&lt;&lt;endl;
          else if(z.imaginary ==1 )
              out&lt;&lt;z.real&lt;&lt;&quot; + &quot;&lt;&lt;&quot;i&quot;&lt;&lt;endl;
          else if(z.imaginary == 0)
              out&lt;&lt;z.real&lt;&lt;endl;
          else if (z.imaginary == -1)
              out&lt;&lt;z.real&lt;&lt;&quot; - &quot;&lt;&lt;&quot;i&quot;&lt;&lt;endl;
          else
              out&lt;&lt;z.real&lt;&lt;&quot; - &quot;&lt;&lt;abs(z.imaginary)&lt;&lt;&quot;i&quot;&lt;&lt;endl;
          }
      else{
          if(z.imaginary==0)
              out&lt;&lt;z.imaginary&lt;&lt;endl;
          else if (z.imaginary ==1)
              out&lt;&lt;&quot;i&quot;&lt;&lt;endl;
          else if (z.imaginary == -1)
              out&lt;&lt;&quot;-i&quot;&lt;&lt;endl;
          else
              out&lt;&lt;z.imaginary&lt;&lt;&quot;i&quot;&lt;&lt;endl;
          }
      return out;
  }
  </code></pre>
  <p>Any explanation would be highly appriciated. Thanks in advance.</p>