我有以下代码。 代码在没有 operator + 部分的情况下工作正常。 它给了我
error: no match for ‘operator=’ in ‘final[i][j] = (((matrix*)this)->matrix::mat[i][j] + matr->matrix::mat[i][j])’
和
error: no match for ‘operator<<’ in ‘std::cout << final[i][j]
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
class matrix {
private :
int i,j;
double mat[2][2];
public :
matrix () {
}
void getdata();
double determinant();
matrix operator + (matrix &);
};
//getdata
void matrix :: getdata() {
cout <<"Please provide a 2x2 matrix :"<<endl;
for (int i=0;i<2;i++) {
for (int j=0;j<2;j++) {
cout <<"Give the elements of the matrix : "<<endl;
cin >> mat[i][j];
}
}
}
//compute determinant
double matrix :: determinant () {
double det;
det = mat[0][0]*mat[1][1] -mat[0][1]*mat[1][0];
cout <<"The determinant of the matrix is :"<<det<<endl;
}
//compute addition
matrix matrix ::operator +(matrix &matr) {
matrix final[2][2];
for (int i=0;i<2;i++) {
for (int j=0;j<2;j++) {
final[i][j]=mat[i][j]+matr.mat[i][j];
}
}
cout <<"The addition of the two matrices is :"<<endl;
for (int i=0;i<2;i++) {
for (int j=0;j<2;j++){
cout << final[i][j];
}
cout <<endl;
}
}
int main()
{
matrix pinakas1,pinakas2;
pinakas1.getdata();
pinakas2.getdata();
pinakas1.determinant();
pinakas2.determinant();
pinakas1+pinakas2;
return 0;
}
最佳答案
那是因为 matrix final[2][2];
声明了一个二维矩阵数组,所以 final[i][j]
是 类型>matrix &
和相关运算符未定义。你的意思一定是double final[2][2];
关于c++ - 添加具有运算符重载的 2x2 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5184209/