I am attempting to create an overloaded operator for a matrix class that I have built. My matrix class stores the matrix in a dynamically allocated multidimensional array. I
i
actually goes from 0
to this->rows - 2
(because of i < n-1
for i = n-1
is false). Same for other loops. This seems not to be correct behaviour for matrices multiplication.P.S. If T is type of matrix elements, then type of sum_elems
should be T
.
As I suspected, your copy constructor and assignment operator are in fact not implemented correctly. You are simply copying the pointer over. That means that when you copy one matrix to another, they both share the same data. When one of them goes out of scope, the destructor is called, then the shared data is deleted, leaving the remaining matrix with dangling pointers.
Fix those functions so they actually allocate new arrays, and copy the data.