I'm working on this Matrix class with operator overloading. For the sake of learning it and brushing up on my C++.

I'm trying overload opertator+ and it seems to be working in a sense that syntax is correct, however my program crashes when I try to multiply two matrices together.

I've added the entire code below. Could someone just help me debug this? I can't seem to figure it out.

Thanks in advance.

#ifndef MATRIX_H #define MATRIX_H #include <stdio.h> #include <math.h> #include "Vector.h" class Matrix { public: ~Matrix(); //Deconstructor unsigned int _rows, _cols; Matrix (unsigned int rows, unsigned int cols); float** pointerMatrix; //pointer to a pointer matrix void assignmentOperator(const Matrix& a); float & operator()(const int r, const int c); //overload operator for () for cell assignment //Basic Functions void print(); //Print Matrix Matrix multiplyVector (Vector vector1); //Matrix multiply by vector Matrix& operator= (const Matrix& a); friend Matrix operator+ (const Matrix& firstMatrix, const Matrix& secondMatrix); //Matris Addition }; #endif

#include "Matrix.h" Matrix::Matrix(unsigned int rows, unsigned int cols) { pointerMatrix = NULL; //check to make sure that rows/cols are greater than zero //however pointless with unsigned ints if (rows > 0 && cols > 0) { _rows = rows; _cols = cols; } pointerMatrix = new float*[rows]; for (int r = 0; r < rows; r++) { pointerMatrix[r] = new float[rows]; for (int c = 0; c < cols; c++) { //initially fill the matrix with all zero values pointerMatrix[r][c] = 0.0f; }//end for cols }//end for rows }//end constructor //assignment operator void Matrix::assignmentOperator(const Matrix& a) { _rows = a._rows; _cols = a._cols; pointerMatrix = new float*[a._rows]; for (int r = 0; r < a._rows; r++) { pointerMatrix[r] = new float[a._cols]; // copy the values from the matrix a for (int c = 0; c < a._cols; c++) { pointerMatrix[r][c] = a.pointerMatrix[r][c]; } } } float& Matrix::operator()(const int r, const int c) { if (pointerMatrix != NULL && r > 0 && r <= _rows && c > 0 && c <= _cols) { return pointerMatrix[r-1][c-1]; } else { throw printf("%c", "Out of Bounds Exception"); } } Matrix& Matrix::operator=(const Matrix& a) { _rows = a._rows; _cols = a._cols; pointerMatrix = new float*[a._rows]; for (int r = 0; r < a._rows; r++) { pointerMatrix[r] = new float[a._cols]; // copy the values from the matrix a for (int c = 0; c < a._cols; c++) { pointerMatrix[r][c] = a.pointerMatrix[r][c]; } } return *this; } Matrix operator+ (const Matrix& firstMatrix, const Matrix& secondMatrix) { // check if the dimensions matc if (firstMatrix._rows == secondMatrix._rows && secondMatrix._cols == secondMatrix._cols) { Matrix result(firstMatrix._rows, firstMatrix._cols); for (int r = 0; r < firstMatrix._rows; r++) { for (int c = 0; c < firstMatrix._cols; c++) { result.pointerMatrix[r][c] = firstMatrix.pointerMatrix[r][c] + secondMatrix.pointerMatrix[r][c]; } } return result; } } void Matrix::print() { if (pointerMatrix != NULL) { printf("\n ["); for (int r = 0; r < _rows; r++) { if (r > 0) { printf(" "); } for (int c = 0; c < _cols-1; c++) { printf("%.2f, ", pointerMatrix[r][c]); } if (r < _rows-1) { printf("%.2f;\n", pointerMatrix[r][_cols-1]); } else { printf("%.2f]\n", pointerMatrix[r][_cols-1]); } } } else { // matrix is empty printf("[ ]\n"); } }//end print function //Deconstructor Matrix::~Matrix() { // clean up allocated memory for (int r = 0; r < _rows; r++) { delete pointerMatrix[r]; } delete pointerMatrix; pointerMatrix = NULL; }

int rows = 3; int cols = 3; int main () { Matrix one = Matrix(rows, cols); Matrix two = Matrix(rows, cols); one(1,1) = 6.0f; //created my own index, starts at 1,1 one(1,2) = 3.0f; //the rest of the elements would have zeros filled in on them Matrix result = one + two; result.print(); system("PAUSE"); return 0; }