I am having trouble getting my addition overloading function too work properly and was wondering if I could get some help with it. The rest of the functions and constructors in the class are default and can not be changed for this project so regardless they are correct. This means the only problems I am having are with the operator overloading functions themselves. Thanks in advance.
Matrix.h:
class Matrix {
// models a matrix of two dimentions, i. e. rows and columns of values
public:
Matrix & operator=(const Matrix& m);
Matrix & operator+(const Matrix& m);
Matrix.cpp:
//not included are the default and copy constructors plus read and write
//functions, etc...
Matrix& Matrix::operator=(const Matrix& m) {
this->rows = m.rows;
this->cols = m.cols;
matrix = vector< vector<double> >(rows);
for (int r=0; r<rows; r++)
matrix[r] = vector<double>(cols);
for (int r=0; r<rows; r++)
for (int c=0; c<cols; c++)
matrix[r][c] = m.matrix[r][c];
return *this;
}
Matrix & Matrix::operator+(const Matrix& m) {
Matrix newMatrix;
newMatrix = vector< vector<double> >(rows);
if (this->rows != m.rows || this->cols != m.cols) {
newMatrix.rows = 0;
newMatrix.cols = 0;
return newMatrix;
}
else {
newMatrix.rows = m.rows;
newMatrix.cols = m.cols;
for (int r = 0; r < m.rows; r++) {
newMatrix.matrix[r] = vector<double>(m.cols);
}
for (int r = 0; r < m.rows; r++) {
for (int c = 0; c < m.cols; c++)
newMatrix.matrix[r][c] = matrix[r][c] + m.matrix[r][c];
}
return newMatrix;
}
}
Main.cpp:
//in main.cpp I am trying to do the following operation and then output the result:
e = a + b;