具有二维数组添加操作的模板
Templates with Operation for 2d array addition
我有两个二维数组 arr1
这属于对象 s1
和 arr2
这属于对象 s2
我想存储添加到对象 s3
。经过大量搜索和试验 this
,这是我的代码:
#include <iostream>
#include <sstream>
using namespace std;
template <class T>
class Matrix
{
private:
T arr[2][2];
T temp_arr[2][2];
public:
Matrix();
void display();
void seter(T _var[2][2]);
Matrix operator + (Matrix tmp)
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
this->temp_arr[i][j]=arr[i][j]+tmp.arr[i][j];
return *this;
}
};
template<class T>
Matrix<T>::Matrix()
{
}
template<class T>
void Matrix<T>::display()
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
cout<<endl<<arr[i][j];
}
template<class T>
void Matrix<T>::seter(T _var[2][2])
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
arr[i][j]=_var[i][j];
}
int main()
{
double arr1[2][2];
double arr2[2][2];
double x=2.5,y=3.5;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
arr1[i][j]=x++;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
arr2[i][j]=y++;
Matrix<double> s1;
Matrix<double> s2;
Matrix<double> s3;
s1.seter(arr1);
s2.seter(arr2);
s3=s1+s2;
s1.display();
cout<<endl;
s2.display();
cout<<endl;
s3.display();
return 0;
}
它仍然是 returns 对象的数组 s1
,我不明白为什么,因为网络上的许多示例都与我的代码相似。
您不应该将 temp_arr
作为 class 成员,而是在您的 const 运算符中使用临时实例并 return 它在堆栈中
此外,由于人们应该能够添加 const
个实例,因此请将您的 operator +
设为 const
成员函数:
Matrix operator + (const Matrix& tmp) const
{
Matrix ret(*this);
ret+=tmp;
return ret;
}
以上应该有助于说明 + 和 += 之间的区别,但您可以对其进行优化。
要解决您的问题,您应该
从 Matrix
class 和 operator +
中删除 temp_arr
。
将 operator +
中的这一行从:this->temp_arr[i][j]=arr[i][j]+tmp.arr[i][j];
更改为:arr[i][j] += tmp.arr[i][j];
您的实施中不需要 temp_arr
。
这是一个使用上述更改的实例:http://ideone.com/lMF3kT
另一个问题是您在调用 operator +
时更改了原始 Matrix
对象。这是违反直觉的,因为 +
不应更改原始对象,而应 returned 一个新对象。
要解决此问题,您可以将代码(一旦修复)移出 operator +
并将其移至 operator +=
。
Matrix& operator += (const Matrix& tmp)
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
arr[i][j] += tmp.arr[i][j];
return *this;
}
请注意,我们return原始对象作为参考。现在,我们可以根据 operator +=
:
来实现 operator +
Matrix operator + (const Matrix& tmp)
{
Matrix temp(*this);
return temp += tmp;
}
编辑:
使参数常量引用。
我有两个二维数组 arr1
这属于对象 s1
和 arr2
这属于对象 s2
我想存储添加到对象 s3
。经过大量搜索和试验 this
,这是我的代码:
#include <iostream>
#include <sstream>
using namespace std;
template <class T>
class Matrix
{
private:
T arr[2][2];
T temp_arr[2][2];
public:
Matrix();
void display();
void seter(T _var[2][2]);
Matrix operator + (Matrix tmp)
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
this->temp_arr[i][j]=arr[i][j]+tmp.arr[i][j];
return *this;
}
};
template<class T>
Matrix<T>::Matrix()
{
}
template<class T>
void Matrix<T>::display()
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
cout<<endl<<arr[i][j];
}
template<class T>
void Matrix<T>::seter(T _var[2][2])
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
arr[i][j]=_var[i][j];
}
int main()
{
double arr1[2][2];
double arr2[2][2];
double x=2.5,y=3.5;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
arr1[i][j]=x++;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
arr2[i][j]=y++;
Matrix<double> s1;
Matrix<double> s2;
Matrix<double> s3;
s1.seter(arr1);
s2.seter(arr2);
s3=s1+s2;
s1.display();
cout<<endl;
s2.display();
cout<<endl;
s3.display();
return 0;
}
它仍然是 returns 对象的数组 s1
,我不明白为什么,因为网络上的许多示例都与我的代码相似。
您不应该将 temp_arr
作为 class 成员,而是在您的 const 运算符中使用临时实例并 return 它在堆栈中
此外,由于人们应该能够添加 const
个实例,因此请将您的 operator +
设为 const
成员函数:
Matrix operator + (const Matrix& tmp) const
{
Matrix ret(*this);
ret+=tmp;
return ret;
}
以上应该有助于说明 + 和 += 之间的区别,但您可以对其进行优化。
要解决您的问题,您应该
从
Matrix
class 和operator +
中删除temp_arr
。将
operator +
中的这一行从:this->temp_arr[i][j]=arr[i][j]+tmp.arr[i][j];
更改为:arr[i][j] += tmp.arr[i][j];
您的实施中不需要 temp_arr
。
这是一个使用上述更改的实例:http://ideone.com/lMF3kT
另一个问题是您在调用 operator +
时更改了原始 Matrix
对象。这是违反直觉的,因为 +
不应更改原始对象,而应 returned 一个新对象。
要解决此问题,您可以将代码(一旦修复)移出 operator +
并将其移至 operator +=
。
Matrix& operator += (const Matrix& tmp)
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
arr[i][j] += tmp.arr[i][j];
return *this;
}
请注意,我们return原始对象作为参考。现在,我们可以根据 operator +=
:
operator +
Matrix operator + (const Matrix& tmp)
{
Matrix temp(*this);
return temp += tmp;
}
编辑:
使参数常量引用。