title: C++ Operator Overloading for a Matrix Class with Both Real and Complex Matrices
body: <p>I'm trying to implement a matrix class that can handle both real and complex matrices.  I'm running into problems when I try to overload the multiplication operator.  Specifically, when I try to multiply a double matrix by a complex matrix (in that order).  The result should be complex, but the * operator is a member of a double matrix in that case, I don't see how to return a complex matrix (I've tried using a friend operator, this doesn't seem to work either).  Here's the relevant code snippet:</p>
  
  <pre><code>template &lt;class V&gt; Matrix&lt;T&gt; operator *(const Matrix&lt;V&gt; &amp;b)
  {
      long i, j, k;  T temp;  Matrix&lt;T&gt; c(mRows, b.Cols());
  
      for (i = 0; i &lt; mRows; i++)
          for (j = 0; j &lt; c.Cols(); j++)
          {
              for (temp = 0, k = 0; k &lt; mCols; k++)
                  temp += this-&gt;Element(i, k)*b.Element(k, j);
  
              c(i, j) = temp;
          }
  
          return c;
  }
  </code></pre>
  
  <p>So, if A is real, and B and C are complex, C = B<em>A works fine, but C = A</em>B doesn't.  Specifically, the compiler error flags the "temp +=" line, saying there is no global operator found which takes type complex (or no acceptable conversion).  I'd like avoid completely specializing the template for real and complex types, is there a way around this?</p>
  
  <p>Thanks in advance.</p>