使用运算符重载添加 2x2 矩阵
我有以下代码。 代码在没有运算符 + 部分的情况下工作正常。 它给了我
错误:'final[i][j] = (((matrix*)this)->matrix::mat[i][j] + matr->matrix: 中的 'operator=' 不匹配:mat[i][j])'
和
错误:与“operator<<”不匹配在 'std::cout <<最终[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;
}
I have the following code.
The code works fine without the operator + part.
It gives me
error: no match for ‘operator=’ in ‘final[i][j] = (((matrix*)this)->matrix::mat[i][j] + matr->matrix::mat[i][j])’
and
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要定义一个 <<矩阵的运算符。
'final' 被定义为 2x2 数组 f 'matrix' (
matrix Final[2][2];
)因此
cout << Final[i][j];
引用一个矩阵对象。You need to define a << operator for your matrix.
'final' is defined as a 2x2 array f 'matrix' (
matrix final[2][2];
)Therefore
cout << final[i][j];
references a matrix object.这是因为
matrix Final[2][2];
声明了一个二维矩阵数组,因此final[i][j]
的类型为matrix & ;
且相关运算符未定义。您的意思一定是double final[2][2];
That's because
matrix final[2][2];
declares a 2-d array of matrices, sofinal[i][j]
is of typematrix &
and the relevant operators aren't defined. You must have meantdouble final[2][2];
您需要将
operator+
编写为,使用
final.mat
来访问实际的数据成员。另外,matrix Final[2][2]
声明matrix
类型的二维数组。它没有做你想做的事!You need to write
operator+
as,Use
final.mat
to access the actual data member. Alsomatrix final[2][2]
declares two dimensional array of typematrix
. It doesn't do what you intend it to do!