Matrix Multiplication with operator overloading

前端 未结 2 392
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 10:49

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

相关标签:
2条回答
  • 2021-01-03 11:17
    1. 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.
    2. Nevertheless, this code fragment seems to be correct. Can you provide us with full class implementation?

    P.S. If T is type of matrix elements, then type of sum_elems should be T.

    0 讨论(0)
  • 2021-01-03 11:26

    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.

    0 讨论(0)
提交回复
热议问题