0

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;
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
tmricks94
  • 35
  • 1
  • 6
  • Could you tell us what your specific problem is? – Ryan T. Grimm Apr 14 '14 at 02:42
  • Did you mean `newMatrix.matrix = vector< vector >(rows);` instead of `newMatrix = vector< vector >(rows);`??? – R Sahu Apr 14 '14 at 02:43
  • [undefined behaviour](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – chris Apr 14 '14 at 02:47
  • @tmricks94 - First, why complicate things by introducing an assignment operator? According to your snippet, all the members are either vectors or vector or basic types. In other words, the compiler-generated assignment operator will do the job just fine without "help", so there is no need to write your own assignment operator . So get rid of it. The same thing goes for any copy-constructor that you may have written. No need for it. – PaulMcKenzie Apr 14 '14 at 02:55

2 Answers2

2

While assignment operators should return a reference to the left object (resp. *this), all other infix operators should return by value. They are most easily written using the respective compound-assignment operator like +=.
For those reason most infix non-assignment-operators are free functions.

General pattern for infix +:

template<typename T> inline const T operator(const T& a, const T& b) {
    T c = a;
    return c += b;
}

You do not want to have such infix-operators as member-functions, because that breaks symmetry.
Instead, just define the compound operator +=.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • *"namespace std already contains these free functions."* - it does? Cool. I couldn't find a reference to that - which header is it in? Thanks. – Tony Delroy Apr 14 '14 at 03:14
  • @TonyD: Just searched, and have not found. That template should really be there. Looks like I pulled it from somewhere else. – Deduplicator Apr 14 '14 at 03:23
2

You're returning a reference to the temporary newMatrix

Instead, your addition operator should return the result by value.

The function signature should look like this:

Matrix operator+(const Matrix& m);