I have a MatrixType
header file which has the following definition :
http://pastebin.com/DMzf1wGB
//Add Two Matrices and store result into another matrix
void Add(MatrixType otherMatrix, MatrixType& resultMatrix);
The implementation of the method above is as such :
void MatrixType::Add(MatrixType otherMatrix, MatrixType& resultMatrix)
{
cout << "Inside Add func!" << endl;
cout << "other matrix : " << endl;
otherMatrix.PrintMatrix();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
resultMatrix.values[i][j] = values[i][j] + otherMatrix.values[i][j];
}
cout << "\n";
resultMatrix.PrintMatrix();
cout << "\n";
}
}
Definition of PrintMatrix()
:
void MatrixType::PrintMatrix()
{
//Pre: None
//Post: Matrix is printed row wise
for (int i = 0; i < numRows; i++) {
cout << "[ ";
for (int j = 0; j < numCols; j++) {
cout << values[i][j];
}
cout << "]";
cout << "\n";
}
}
Now in my Main.cpp
I have MatrixType
array like this : MatrixType matrixStore[10]
to store 10 MatrixType objects. The complete code for Main.cpp is here : http://pastebin.com/aj2eEGVS
int rows = matrixStore[index1].getRowSize();
int cols = matrixStore[index1].getColSize();
cout << "The two matrices can be added!" << endl;
cout << "Computing... " << endl;
//Create Result Matrix and a pointer variable to it
MatrixType pResultMatrix = MatrixType(rows, cols);
matrixStore[resultIndex] = pResultMatrix;
//Invoke Add function
matrixStore[index1].Add(matrixStore[index2], matrixStore[resultIndex]);
Now when in my Add() function I do otherMatrix.PrintMatrix()
it prints out the values of the matrix invoking the Add()
function. And due to this => either I do not have reference to the matrix object invoking the method or the matrix object being passed as the parameter!
Also whenever I do PrintMatrix()
after I have added values (in the next round of Switch Case), I always get junk values.
Any solution/explanation to this ?
TIA