I am having a hard time figuring out why my Overloaded operation does not return what I am looking for. If I display my matrix right after the calculations, it works just fine. I cannot figure out why I can't return the class and display the matrix. Is my destructor deleting the pointer in my class when it gets returned? (I do not think that would cause a shallow copy?) I just cannot see what I am missing. Thank you in advance guys.
#include "matrix.h"
matrixClass m1;
matrixClass m2;
matrixClass m3;
int main()
{
int rows, columns;
cout << "Enter numbers of rows\n";
cin >> rows;
m1.setRows(rows);
cout << "Enter number of columns\n";
cin >> columns;
m1.setColumns(columns);
m2.setRows(m1.getRows());
m2.setColumns(m1.getColumns());
m3.setRows(m1.getRows());
m3.setColumns(m1.getColumns());
cout << "Enter the elements for the first matrix\n";
m1.setMatrix();
cout << "Enter the elements of the second matrix\n";
m2.setMatrix();
m3 = m1 * m2;
m1.displayMatrix();
m2.displayMatrix();
m3.displayMatrix();
}
You don't provide a copy constructor or assignment operator. So when you perform these operations, you get the default, which is copies of all your members.
Some of your members are pointers, so you get two instances pointing to the same stuff. When one goes, it deletes them, leaving the other pointing to delete memory, dangling references. If the heap reuses those addresses, it'll corrupt the state of that remaining instance.