如何解决永恒赋值运算符调用循环C ++模板类
我有一个矩阵类,其中包括其成员函数的子集,
#ifndef _CMATRIX_CUH_
#define _CMATRIX_CUH_
#include <thrust/device_vector.h>
#include <assert.h>
template<class T>
class CMatrix
{
private:
size_t n_rows,n_cols;
T **data;
__host__ __device__ void allocate_data();
__host__ __device__ void deallocate_data();
public:
__host__ __device__ CMatrix(const size_t n_rows);
__host__ __device__ CMatrix(const size_t n_rows,const size_t n_cols);
__host__ __device__ CMatrix(const CMatrix &other);
__host__ __device__ ~CMatrix();
__host__ __device__ CMatrix& operator=(const CMatrix &rhs);
}
/****************************************************************************************
* Name : CMatrix
* Function : Class constructor,initializes parameters and variables
* Method :
* Author :
* Modified :
*****************************************************************************************/
template <class T>
__host__ __device__ CMatrix<T>::CMatrix(
const size_t n_rows // In: Amount of matrix rows
) :
n_rows(n_rows),n_cols(n_rows)
{
allocate_data();
}
template <class T>
__host__ __device__ CMatrix<T>::CMatrix(
const size_t n_rows,// In: Amount of matrix rows
const size_t n_cols // In: New amount of matrix columns
) :
n_rows(n_rows),n_cols(n_cols)
{
allocate_data();
}
template <class T>
__host__ __device__ CMatrix<T>::CMatrix(
const CMatrix<T> &other // In: Matrix/vector to copy
) :
n_rows(other.n_rows),n_cols(other.n_cols)
{
allocate_data();
for (size_t i = 0; i < n_rows; i++)
{
for (size_t j = 0; j < n_cols; j++)
{
data[i][j] = other.data[i][j];
}
}
}
/****************************************************************************************
* Name : ~CMatrix
* Function : Class destructor
* Method :
* Author :
* Modified :
*****************************************************************************************/
template <class T>
__host__ __device__ CMatrix<T>::~CMatrix()
{
deallocate_data();
}
/****************************************************************************************
* Name : operator=
* Function :
* Author :
* Modified :
*****************************************************************************************/
template <class T>
__host__ __device__ CMatrix<T>& CMatrix<T>::operator=(
const CMatrix<T> &rhs // In: Right hand side matrix/vector to assign
)
{
if (this == &rhs)
{
return *this;
}
deallocate_data();
return *this = CMatrix<T>(rhs);
}
/****************************************************************************************
Private functions
****************************************************************************************/
/****************************************************************************************
* Name : allocate_data
* Function :
* Author :
* Modified :
*****************************************************************************************/
template <class T>
__host__ __device__ void CMatrix<T>::allocate_data()
{
assert(n_rows > 0 && n_cols > 0);
data = new T*[n_rows];
for (size_t i = 0; i < n_rows; i++)
{
data[i] = new T[n_cols];
}
}
/****************************************************************************************
* Name : deallocate_data
* Function :
* Author :
* Modified :
*****************************************************************************************/
template <class T>
__host__ __device__ void CMatrix<T>::deallocate_data()
{
if (data == nullptr)
{
return;
}
for (size_t i = 0; i < n_rows; i++)
{
delete[] data[i];
}
delete[] data;
data = nullptr;
}
#endif
当我在main()函数中调用赋值运算符时
CMatrix<double> t1(2);
CMatrix<double> t2(3);
t1 = t2;
由于赋值运算符一遍又一遍地调用自身并且永不返回,因此堆栈出现分段错误,为什么?我不知道经过长时间调试后是什么原因造成的。这不是三本教科书吗?我正在使用const引用,并从分配中返回引用。是的,我知道我可以使用std ::等,问题是在我知道之后,在Cuda中是不可能的。
解决方法
赋值运算符CMatrix :: operator =递归调用自己。
template <class T>
__host__ __device__ CMatrix<T>& CMatrix<T>::operator=(
const CMatrix<T> &rhs // In: Right hand side matrix/vector to assign
)
{
...
return *this = CMatrix<T>(rhs); // calls operator=
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。