I'm trying to write a matrix program in C++ and I get the error invalid operands to binary expression. I know it has to do with my overloaded operator with "*". In my code main.cpp:
Matrix mat7=mat1*3;
cout<<"Mult m1 * 3;"<<endl;
print_matrix(mat7);
In this part I am multiplying matrix 1 by 3.
What I need help on is in my matrix.cpp
Matrix Matrix::operator * (const Matrix& mat2){
if(this->num_cols==mat2.num_rows){
Matrix result(num_rows, mat2.num_cols);
for(int i=0; i<num_rows; i++){
for(int j=0; j<mat2.num_cols; j++){
result.matrix[i][j]=0;
for(int k=0; k<this->num_cols; k++){
result.matrix[i][j]+=matrix[i][k]*mat2.matrix[k][j];
}
}
}
return result;
}
else{
Matrix result(num_rows, num_cols);
for(int i=0; i<num_rows; i++){
for(int j=0; j<num_cols; j++){
result.matrix[i][j]=0;
}
}
return result;
}
}
This for my multiplication operator overload to multiplying another matrix.
My question is how do I do the same but with an integer?
I think defining a method Matrix Matrix::operator* (int factor)
should do what you're looking for.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.