【问题标题】:Templates with Operation for 2d array addition具有二维数组添加操作的模板
【发布时间】:2015-06-17 19:48:46
【问题描述】:

我有两个二维数组arr1 这属于对象s1arr2 这属于对象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;
 }

它仍然返回对象s1的数组,我不知道为什么,因为网络上的许多示例都与我的代码相似。

【问题讨论】:

  • Matrix operator + (Matrix tmp) Operator + 应该返回一个全新的Matrix,而不是现有的矩阵。返回现有矩阵将是operator += 的工作。
  • @NathanOliver 我看不出三法则与此有什么关系。
  • @juanchopanza 我收回了它。当问题是 Y 时看到 X。

标签: c++ arrays templates operator-overloading


【解决方案1】:

要解决您的问题,您应该

  1. Matrix 类和operator + 中删除temp_arr

  2. operator + 中的这一行从这个:this-&gt;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 对象。这是违反直觉的,因为+ 不应更改原始对象,而应返回一个新对象。

要解决此问题,您可以将代码(一旦修复)移出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;
}

请注意,我们返回原始对象作为参考。现在,我们可以用operator += 来实现operator +

Matrix operator + (const Matrix& tmp)
{
    Matrix temp(*this);
    return temp += tmp;
}

编辑:

将参数设为 const 引用。

【讨论】:

  • 这不是operator += 吗?
  • 我试过了,但我不希望 s1 对象的数组发生变化。更改后它完全按照我的意愿工作!
  • 有两个问题,第一个是计算错误,第二个是算子有问题。答案解决了这两个问题。
  • 不改变数据成员的操作符一般设为const
【解决方案2】:

您不应该将 temp_arr 作为类成员,而是在 const 运算符中使用临时实例并将其返回到堆栈中

此外,既然人们应该能够添加 const 实例,请将您的 operator + 设为 const 成员函数:

Matrix operator + (const Matrix& tmp) const
{
    Matrix ret(*this);
    ret+=tmp;
    return ret;
}

以上内容应该有助于说明 + 和 += 之间的区别,但您也许可以对其进行优化。

【讨论】:

  • 我先尝试了这个,然后我认为this 应该解决我的问题,因为我很难返回temp_arr。我尝试添加 Matrix(T _temp[2][2]); 来传递新数组,但它不起作用。
  • 实际上没有,但我正在努力让它发挥作用。您正在创建 ret 并添加 tmp ,但我想添加 tmparr。而且我也没有运营商+= ,我必须做一个吗?
  • 您不能更改该数组中的数据成员。您应该将两者作为练习来查看差异,但如果您愿意,您可以将其更改为与设置成员数组相同的循环
  • 我赞成这个答案也是因为它是解决方案的一部分。我不知道我需要另一个操作员。
猜你喜欢
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
  • 2012-03-05
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多