C ++运算符+和运算符+ =重载 [英] C++ operator+ and operator+= overloading

查看:200
本文介绍了C ++运算符+和运算符+ =重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c ++中实现自己的矩阵类,以帮助我发展我对语言的理解。我读某处,如果你有一个工作的+ =运算符,在你的+运算符中使用它。这是我得到的:

 模板< class T& 
const Matrix< T> Matrix< T> :: operator +(const Matrix& R){

Matrix< T> copy(* this);
return copy + = R;
}

这里是+ =运算符重载:

 模板< class T> 
const Matrix< T> Matrix< T> :: operator + =(const Matrix& second_matrix){
//了解如何抛出错误...
if(rows!= second_matrix.getNumRows cols!= second_matrix.getNumCols()){throwDimension mismatch。;}
int i,j;
for(i = 0; i< rows; i ++){
for(j = 0; j data [i] [j] + = second_matrix .get(i,j);
}
}
return * this;
}

我可以使用+ =只是罚款(例如,a + = b;返回没有错误)。但是调用+运算符(例如,a = b + c;)会返回:

  test.cpp.out(77055) :对象的***错误0x300000004:未释放指针

为了完整性,这里是我的析构函数:

  template< class T> 
Matrix< T> ::〜Matrix(){
for(int i = 1; i< rows; i ++){
delete [] data [i] }
delete [] data;
}

我一直使用C ++几年,有时难以跟踪指针。我希望这是正常的...
任何帮助将是伟大的。谢谢!



编辑:这里是我的拷贝构造函数。它设置为释放数据数组,但我删除了。现在我得到分割错误。

 模板< class T& 
Matrix< T> :: Matrix(const Matrix< T>& second_matrix){

rows = second_matrix.getNumRows();
cols = second_matrix.getNumCols();
data = new T * [rows];

int i,j;
for(i = 0; i< rows; i ++){
data [i] = new T [cols];
}
for(i = 0; i for(j = 0; j data [i] j] = second_matrix.get(i,j);
}
}

}


方案

运算符+()不应返回引用类型,因为它是一个保存操作结果的新(本地声明的) p>

I'm implementing my own matrix class in c++ to help me develop my understanding of the language. I read somewhere that if you've got a working += operator, to use it in your + operator. So that's what I've got:

template <class T>
const Matrix<T>& Matrix<T>::operator+(const Matrix<T> &R){

    Matrix<T> copy(*this);
    return copy += R;
}

And here is the += operator overload:

template <class T>
const Matrix<T>& Matrix<T>::operator+=(const Matrix<T> & second_matrix){
    //Learn how to throw errors....
    if (rows != second_matrix.getNumRows() || cols != second_matrix.getNumCols()){throw "Dimension mismatch.";}
    int i,j;
    for (i = 0; i < rows; i++){
        for (j = 0; j < cols; j++){
            data[i][j] += second_matrix.get(i,j);
        }
    }
    return *this;
}

I can use the += just fine (eg, a += b; returns no errors). But calling the + operator (eg, a = b + c;) returns :

test.cpp.out(77055) malloc: *** error for object 0x300000004: pointer being freed was not allocated

Just for completeness, here's my destructor:

template <class T>
Matrix<T>::~Matrix(){
    for (int i = 1; i < rows; i++){
        delete[] data[i]; }
    delete[] data;
}

I've been using C++ for a couple years on and off, and still have trouble sometimes keeping track of pointers. I hope that's normal... Any help would be great. Thanks!

EDIT: here's my copy constructor. It was set to free the data arrays but i removed that. now I get segmentation faults.

template <class T>
Matrix<T>::Matrix(const Matrix<T>& second_matrix){

    rows = second_matrix.getNumRows();
    cols = second_matrix.getNumCols();
    data = new T*[rows];

    int i,j;
    for (i = 0; i < rows; i++){
        data[i] = new T[cols];
    }
    for (i = 0; i < rows; i++){
        for (j = 0; j < cols; j++){
            data[i][j] = second_matrix.get(i,j);
        }
    }

}

解决方案

operator+() should not return a reference type as it is a new (locally declared) instance that holds the result of the operation.

这篇关于C ++运算符+和运算符+ =重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆