Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This has nothing to do with "calling object C".</p> <p>The first version </p> <pre><code>Matrix C = A * B; </code></pre> <p>uses <em>constructor(s)</em> or class <code>Matrix</code> to initialize new object <code>C</code>.</p> <p>The second version </p> <pre><code>C = C * C; </code></pre> <p>uses <em>assignment</em> operator of class <code>Matrix</code> to assign new value to an existing object <code>C</code>.</p> <p>You managed to screw up the assignment operator declaration/implementation somehow (which you don't show in the code you posted), which is why the second version does not compile.</p> <p>There are problems with your <code>operator *</code> declaration as well. Even if you want to have it as class member, a more meaningful way to declare it would be </p> <pre><code>Matrix Matrix::operator* (const Matrix&amp; B) const { ... </code></pre> <p>Note how <code>const</code> qualifiers are placed. </p> <p><strong>EDIT:</strong> So, here's your problem. Your assignment operator is completely broken.</p> <p>Firstly, you declared your assignment operator as</p> <pre><code>Matrix Matrix::operator=(Matrix&amp; B) </code></pre> <p>This operator cannot accept temporary objects on the right-hand side because you failed to declare the parameter as <code>const</code>. Non-const references cannot be bound to temporary objects. And in <code>C = C * C</code> the right-hand side of assignment is actually a temporary object produced by <code>*</code>operator.</p> <p>Redeclare your assignment operator as </p> <pre><code>Matrix &amp;Matrix::operator=(const Matrix&amp; B) </code></pre> <p>Note, it accepts a <code>const</code> reference and returns a reference.</p> <p>Secondly, your assignment operator is supposed to assign to <code>*this</code>, not to some standalone temporary object. And it is supposed to return a reference to <code>*this</code>. In other words, the implementation should be something along the lines of</p> <pre><code>Matrix &amp;Matrix::operator=(const Matrix&amp; B){ // Resize `*this` to match the size of `B` for(int i = 0; i &lt; M; i++){ for(int j = 0; j &lt; N; j++){ this-&gt;data[i*N+j] = B(i,j); } } return r; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload