You can't modify m1 cause you declare it to be const. It should return reference to m1 as well, not return a copy, otherwise to use your code properly you would need to do.
matResult = (m1 += m2); // m1 isn't modified
Obviously this code doesn't behave like anything else in C++.
//ADDITION ASSIGNMENT
Matrix operator+= ( const Matrix& m1, const Matrix& m2)
{
try
{
if (m1.m_nRows != m2.m_nRows)
{
throw 100;
}
if ( m1.m_nCols != m2.m_nCols)
{
throw 200;
}
}
catch (int e)
{
std::cout << e << " Incompatible matrices for += ." << std::endl;
}
// Matrix temp (m1.m_nRows, m1.m_nCols);
for (int i = 0; i < m1.m_nRows; i++)
{
for (int j = 0; j < m1.m_nCols; j++)
m1.m_dValue[i][j] = m1.m_dValue[i][j] + m2.m_dValue[i][j]; // try '+=' as an alternative
}
return m1;
}
Note:
a) Creating Matrix temp is unnecessary because it is m1 that's being modified, hence the const address.
b) m_dValue[i][j] is the underlying 2-d array in my code and this appears to be what you have (maybe) missed.