Hi everyone. I am writing a program using dynamic 2D arrays to manipulate matrices. I am currently overloading the operators and I am getting segmentation faults. I have a feeling the problem may be the same thing in all of them and if I get help fixing one I could fix the others, but I will post all three of them anyways.
matrixType matrixType::operator+ (matrixType right){
matrixType matrixSum;
if(columnSize != right.columnSize || rowSize != right.rowSize)
cout << "These two matrices may not be added together because they are not the same size" << endl;
matrixSum.matrix = newint*[columnSize];
for(int n = 0; n < columnSize; n++)
matrixSum.matrix[n] = newint[rowSize];
for(int i = 0; i < columnSize; i++){
for(int j = 0; j < rowSize; j++)
matrixSum.matrix[i][j] = (matrix[i][j] + right.matrix[i][j]);
}
return matrixSum;
}
here is my operator- overloading function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
matrixType matrixType::operator- (matrixType right){
matrixType matrixEnd;
matrixEnd.matrix = newint*[columnSize];
for(int n = 0; n < columnSize; n++)
matrixEnd.matrix[n] = newint[rowSize];
if(columnSize== right.columnSize && rowSize == right.rowSize){
for(int i =0; i < columnSize; i++){
for(int j= 0; j < rowSize; j++)
matrixEnd.matrix[i][j] = matrix[i][j] - right.matrix[i][j];
}
}
else
cout << "Matrices are not the same size. They can not be subtracted from eachother."<< endl;
return matrixEnd;
}
and finally here is my operator* overloading function
matrixType matrixType::operator* (matrixType right){
matrixType matrixProduct;
int sum = 0;
matrixProduct.matrix = newint*[matrixProduct.columnSize];
for(int m = 0; m < matrixProduct.columnSize; m++)
matrixProduct.matrix[m] = newint[matrixProduct.rowSize];
if(rowSize == right.columnSize){
matrixProduct.rowSize = right.rowSize;
matrixProduct.columnSize = columnSize;
for (int i = 0; i < matrixProduct.columnSize; i++){
for(int j = 0; j < matrixProduct.rowSize; j++){
for(int n = 0; n < rowSize; n++)
sum = sum + (matrix[i][n] * right.matrix[n][j]);
matrixProduct.matrix[i][j] = sum;
sum = 0;
}
}
}
else
cout << "Matrices are not the right sizes to be multiplied." << endl;
return matrixProduct;
}
I have a feeling it has something to do with setting up my dynamic arrays but I have been tinkering around with it for hours now and can not figure it out. It will compile, but the first time I call on one in the main function it dumps the core due to a segmentation fault. Any help is much appreciated. Thank you!
You seem to have a constructor, but manually construct the temporary matrix manually. You are certainly missing a copy constuctor and assignment operator. You'll need those along with a destructor as you're manipulating dynamic memory.
What does your constructor code and test program look like? I'd like to see how the test matrices are constructed.
The assignment operators have been overloaded and work fine.
My other constructor is this one (the assignment calls for the default size to be 10x10):
1 2 3 4 5 6 7 8 9 10 11
matrixType::matrixType (int rows, int columns){
columnSize = columns;
rowSize = rows;
matrix = newint*[columnSize];
for (int i = 0; i < columnSize; i++){
matrix[i] = newint[rowSize];
for(int j = 0; j < rowSize; j++)
matrix[i][j] = 0;
}
}
my matrices are constructed from a data file. The sizes are input correctly and the three matrices that I do assign from the data file. There are two more matrices that I have the correct column size and row size but i need them to have their data input by either adding two of the matrices or multiplying two of them.
So if what i think you're saying is right, because i have the constructors, I don't need to dynamically create the 2-d array because it is done in the constructor?
**EDIT** I am trying to work off of that clue but it still doesnt seem to be working. Any tips on how i might set up an operator overload in this situation?
// you pass by value, invoking a copy; should be const ref
matrixType matrixType::operator+ (matrixType right){
// why not specify the size on construction?
matrixType matrixSum;
// you can throw an exception if the size is wrongif(columnSize != right.columnSize || rowSize != right.rowSize)
cout << "These two matrices may not be added together because they are not the same size" << endl;
// you've already got an initialised matrix, leaking the original matrix
matrixSum.matrix = newint*[columnSize];
for(int n = 0; n < columnSize; n++)
matrixSum.matrix[n] = newint[rowSize];
for(int i = 0; i < columnSize; i++){
for(int j = 0; j < rowSize; j++)
matrixSum.matrix[i][j] = (matrix[i][j] + right.matrix[i][j]);
}
// invoke the copy constructorreturn matrixSum;
}
I can see the memory leak, but I don't see why it crashes. What does setMatrix() do?