Operater Overloading for Matrices

Sep 16, 2014 at 9:06am
Hey guys, I want to implement operator overloading for +=, so that the following arethmetic is possible for matrices: matrix += matrix

Here is how I have defined it in matrix.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef MATRIX_H
#define MATRIX_H

#include <cassert>
#include <iostream>
#include <iomanip>

using namespace std;

template <class T> class Matrix;
template <class T> Matrix<T> operator+= (const Matrix<T>& m1, const Matrix<T>& m2);

template <class T>
class Matrix
{
        ...
	//Overloaded += operator signature
	friend Matrix<T> operator+= <>(const Matrix<T>& m1, const Matrix<T>& m2);
        ...
public:
	...

private:
	...
};
#endif 


Here is my attempt at the implementation in matrix.cpp
However this is incorrect

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template <class T>
Matrix<T> operator+= (const Matrix<T>& m1, const Matrix<T>& m2)
{
    Matrix<T> temp = new T*[Matrix<T>::getRows()];
    for (unsigned int i = 0; i < Matrix<T>::getRows(); ++i)
    {
	    temp[i] = new T[Matrix<T>::getCols()];
    }
  
    for (unsigned int i = 0; i < Matrix<T>::getRows(); i++)
    {
	for (unsigned int j = 0; j < Matrix<T>::getCols(); j++)
	{
	    m1[i][j] += m2[i][j];
	}
    }
    
    return m1;
}


How do I implement this correctly? Any help would be greatly appreciated.
Last edited on Sep 16, 2014 at 9:07am
Sep 16, 2014 at 9:36am
Templates should go in headers. Not in implementation files.
Sep 16, 2014 at 10:16am
This is not relevant, please help with the implementation.
Sep 16, 2014 at 10:31am
You probably want to store the sum in the temp matrix and also return temp from the function.
Sep 16, 2014 at 10:36am
Line 4: It is essentually Matrix<T> = T** What happens here? is there constructor which takes pointer to pointer?

Matrix<T>::getRows() Why getRows() is a static function? What its purpose?
Sep 16, 2014 at 11:06am
closed account (48T7M4Gy)
Try this source. What u want is there provided you are prepared to wade through it.
http://www.techsoftpl.com/matrix/download.php
Sep 16, 2014 at 1:26pm
closed account (10X9216C)
 
Matrix<T> operator+= (const Matrix<T>& m1, const Matrix<T>& m2)

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++.
Last edited on Sep 16, 2014 at 1:27pm
Sep 18, 2014 at 6:28am
closed account (48T7M4Gy)
@OP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//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.
Last edited on Sep 18, 2014 at 6:31am
Topic archived. No new replies allowed.