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:
template <class V> Matrix<T> operator *(const Matrix<V> &b)
{
long i, j, k; T temp; Matrix<T> c(mRows, b.Cols());
for (i = 0; i < mRows; i++)
for (j = 0; j < c.Cols(); j++)
{
for (temp = 0, k = 0; k < mCols; k++)
temp += this->Element(i, k)*b.Element(k, j);
c(i, j) = temp;
}
return c;
}
So, if A is real, and B and C are complex, C = BA works fine, but C = AB 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?
Thanks in advance.