c++ - 添加具有运算符重载的 2x2 矩阵

标签 c++ matrix operator-keyword

我有以下代码。 代码在没有 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/

相关文章:

c# - 矩阵对象之间的距离,相对于 parent 的方向,使用 XNA?

c - C语言中如何判断/运算符是否无余?

c++ - 按钮 onClick 的工作原理

c++ - 超定线性代数方程的最小二乘解Ax = By

c# - C++/C# 应用程序连接到 Linux 服务器

c++ - 当对象包含指针成员时,堆栈上对象破坏的段错误?

r - 在有约束的情况下扩展电网(或电源组)

java - 如何使用数组创建随机矩阵

c++ - ostream 运算符<< 调用父ostream

具有方法调用的表达式中的 Java 后缀运算符优先级