I need to implement a matrix class for my study, and now I'm stuck with operator + overloading.
Here's my matrix.cpp code:
template<class type>
matrix<type> matrix<type>::operator+ (const matrix<type>& matObj){
matrix<type> newMat(this->WIDTH, this->HEIGHT, 0);
    for (int i = 0; i < this->HEIGHT; i++)
        for (int j = 0; j < this->WIDTH; j++)
            newMat.array[WIDTH * i + j] = matObj.array[WIDTH * i + j] + this->array[WIDTH * i + j];
    return newMat;
}
And here's main.cpp code:
int main() {
    vector< vector<int>> v = {
        {1, 2, 3},
        {4, 5, 6},
        {1, 2, 9}
    };
    math::matrix<int> mat1(v), mat2;
    mat2 = mat1+mat1;
    for (int i = 0; i < mat2.cols();  i++) {
        for (int j = 0; j < mat2.rows(); j++) {
            cout << mat2[i][j] << ", ";
        }
        cout << endl;
    }
    cin.get();
    return 0;
}
Assume that all constructors and other codes are written correctly. So the problem is when I try to run the program, it fails on line:
mat2 = mat1+mat1;
And when I try to print newMat in matrix.cpp before the return statement, it prints it correctly. And of course, I have implemented = operator which works fine. Any suggestions?
 
    