thanks for the very constructive input!
as for now, I got it working as far as I can see it. Now my constructors are the same, I just fixed the allocation that was not initialized to 0 and added the copy constructor:
and also the overloaded assingment operator:Code:Matrix::Matrix(Matrix &cSource) { m_nRowCount = cSource.m_nRowCount; m_nColCount = cSource.m_nColCount; m_pfMatrix = new float[m_nRowCount * m_nColCount]; for(int nRow = 0; nRow < m_nRowCount; nRow++) { for(int nCol = 0; nCol < m_nColCount; nCol++) m_pfMatrix[nRow * m_nColCount + nCol] = cSource(nRow, nCol); } }
so that my multiplication algorithm now looks like this:Code:Matrix& Matrix::operator=(Matrix &cSource) { if(this == &cSource) return *this; m_nRowCount = cSource.m_nRowCount; m_nColCount = cSource.m_nColCount; for(int nRow = 0; nRow < m_nRowCount; nRow++) { for(int nCol = 0; nCol < m_nColCount; nCol++) m_pfMatrix[nRow * m_nColCount + nCol] = cSource(nRow, nCol); } return *this; }
and it works like a charm as far as I can tell.Code:Matrix operator*(Matrix &cObj1, Matrix &cObj2) { Matrix pfaResMat(cObj1.m_nRowCount, cObj2.m_nColCount); int i, k, j; for(i = 0; i < pfaResMat.m_nRowCount; i++) { for(j = 0; j < pfaResMat.m_nColCount; j++) { pfaResMat(i, j) = 0; for(k = 0; k < cObj1.m_nColCount; k++) pfaResMat(i, j) += cObj1(i, k) * cObj2(k, j); } } return pfaResMat; }
all other overloaded operators( (), *(by int or float), +) also work perfectly well.
if you see some flaws, leaks or anything that should be inprooved, I would be very happy to know
regards,
replax





Reply With Quote
