gpt4 book ai didi

C++运算符重载

转载 作者:行者123 更新时间:2023-11-30 02:31:58 27 4
gpt4 key购买 nike

我不知道为什么定义运算符重载函数后会出错。错误是:no match for 'operator=' (operand types are 'Matrix' and 'Matrix'),a1+a4 after operator + overload函数只有一个返回的矩阵对象。所以这里应该是 a1 = 对象矩阵?

#include<iostream>
using namespace std;

//write your code here
class Matrix{
private:
int arr[3][3];
public:
friend istream& operator >> (istream& in,Matrix &matrix);
friend ostream& operator << (ostream& out,const Matrix& matrix);
friend Matrix operator + (Matrix &a,Matrix &b);
void setArr(int i,int j,int data){
arr[i][j] = data;
}
int getArr(int i,int j){
return arr[i][j];
}
Matrix& operator = (Matrix &b);
};
istream& operator >> (istream& in,Matrix &matrix)
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
in >> matrix.arr[i][j];
return in;
}
ostream& operator << (ostream& out,const Matrix &matrix){
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
out << matrix.arr[i][j] << " ";
out << endl;
}
return out;
}
Matrix operator + (Matrix &a,Matrix &b){
Matrix temp;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
temp.setArr(i,j,(a.getArr(i,j)+b.getArr(i,j)));
return temp;

}
Matrix& Matrix::operator = (Matrix &b){
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j] = b.getArr(i,j);
return *this;
}
int main()
{
Matrix a1,a2,a3,a4;
cin >> a1;
cin >> a2;
a4 = a2;
a3 = (a1+a4); //an error over there
cout << a1 << endl;
cout << a2 << endl;
cout << a3 << endl;
return 0;
}

最佳答案

问题是您的赋值运算符当前采用 const Matrix引用。然而,(a1+a4)生成一个不能绑定(bind)到- const 的临时对象引用。请参阅以下修复程序以编译您的代码。

#include<iostream>
using namespace std;

//write your code here
class Matrix{
private:
int arr[3][3];
public:
friend istream& operator >> (istream& in,Matrix &matrix);
friend ostream& operator << (ostream& out,const Matrix& matrix);
friend Matrix operator + (Matrix &a,Matrix &b);
void setArr(int i,int j,int data){
arr[i][j] = data;
}
int getArr(int i,int j) const{
// ^^^^^ // Added const
return arr[i][j];
}
Matrix& operator = (const Matrix &b);
// ^^^^^ // Added const
};
istream& operator >> (istream& in,Matrix &matrix)
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
in >> matrix.arr[i][j];
return in;
}
ostream& operator << (ostream& out,const Matrix &matrix){
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
out << matrix.arr[i][j] << " ";
out << endl;
}
return out;
}
Matrix operator + (Matrix &a,Matrix &b){
Matrix temp;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
temp.setArr(i,j,(a.getArr(i,j)+b.getArr(i,j)));
return temp;

}
Matrix& Matrix::operator = (const Matrix &b){
// ^^^^^ // Added const
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j] = b.getArr(i,j);
return *this;
}
int main()
{
Matrix a1,a2,a3,a4;
cin >> a1;
cin >> a2;
a4 = a2;
a3 = (a1+a4); // No errors now
cout << a1 << endl;
cout << a2 << endl;
cout << a3 << endl;
return 0;
}

Live example

注意: getArr必须使函数 const 正确,以便它可以被 const Matrix& 调用赋值运算符中的参数。

关于C++运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36949531/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com