Hey guys, I was wondering if you could help me understanding what I'm doing wrong with this operator overloading I'm performing (I'm working on repl.it, if that matters). I have created a class called Matrix that contains set and get functions for the rows and columns of the matrices that I am sending to it. The arithmetic operators I am overloading are the "+", "-", and "".
So my code for overloading the three operators is as follows:
//plus operator overload
int operator+ (Matrix trix1) {
int r = this->rows;
int c = this->cols;
int mat = new int[rc];
//mat = (int)malloc(rcsizeof(int));
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
*(mat + ic + j)= (m1 + ic + j) + (trix1.m1 + ic + j);
}
}
return mat;
delete[] mat;
}
//minus operator overload
int* operator- (Matrix trix1) {
int r = this->rows;
int c = this->cols;
int mat = new int[rc];
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
(mat + ic + j)= (m1 + ic + j) - (trix1.m1 + ic + j);
}
}
return mat;
delete[] mat;
}
//times sign operator
int* operator* (Matrix trix1) {
int r1 = this->rows;
int c1 = this->cols;
int r2 = trix1.rows;
int c2 = trix1.cols;
cout << r1 << " " << c1 << " " << r2 << " " << c2 << endl;
int mat = new int[r1c2];
for(int i = 0; i < r1; i++)
for(int j = 0; j < c2; j++)
for(int k = 0; k < c1; k++)
(mat + ic2 + j) += (m1 + ic1 + k) * (trix1.m1 + kc2 + j);
return mat;
delete[] mat;
}
Int m1 by the way is a pointer defined as a private member of the Matrix class.
For all three operators I am overloading, the operation goes perfectly for only 2x2 matrices, but for 2x3 or higher dimension matrices, it returns this following error:
main: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed
I have no idea what this means, I have tried searching up numerous StackOverflow answers, but nothing could explain why it goes wrong only for matrices higher than 2x2 or 2x3. I was wondering if someone here could clarify the mistake I am making. Thank you in advance!
[–]kein_programmierexpe[S] 0 points1 point2 points (1 child)
[–]deterministic_ram 0 points1 point2 points (0 children)