Sorry if I sound like a dumbass but I just started coding in C++ and am taking time figuring out some stuff.
Suppose I make a change in the code:
Code:
matrix matrix::operator=(const matrix &mat2);
to
Code:
matrix matrix::operator=(matrix &mat2);
i.e I am removing the const from the parameter being passed to the function. I get errors at all the places where:
<Object of type matrix>=<Expression involving object of type matrix>
e.g:
Code:
k1=dt*(linv*u-linv*r*i);
Is this because the overloaded functions "*","+" etc return an local object temp of class matrix? Since this object does not exist outside the function, what is returned to the R.H.S of the code above is a const object of class matrix. Therefore, must the "=" overloaded function must have a const matrix & in the parameter list? An answer to this might help me understand how these objects are dealt with.
This is the code for the "*" operator by the way.
Code:
matrix operator*(const matrix &mat1, const matrix &mat2) {
int x, y, z;
float sum;
matrix temp;
// Checking for compatibility - columns of left matrix equal to rows of right matrix.
if (mat1.columns!=mat2.rows) {
cout << "Matrices are not compatible.\n";
exit(1);
}
// Performing multiplication.
for (x=0;x<mat1.rows;x++) {
for (z=0;z<mat2.columns;z++) {
sum=0.0;
for (y=0;y<mat1.columns;y++) {
sum=sum+mat1.mat_var[x][y]*mat2.mat_var[y][z];
}
temp.mat_var[x][z]=sum;
}
}
// Defining dimensions of the product.
temp.rows=mat1.rows;
temp.columns=mat2.columns;
return temp;
}
Thanks.