C++ 使用 unique_ptr、make_unique 的已删除函数
C++ use of deleted function with unique_ptr, make_unique
伪class执行矩阵加法,使用std::unique_ptr作为数据成员来表示元素。 M 行,N 列。 Class 是类型 M、N 的模板。我正在尝试执行矩阵加法 (C = A+B) 并不断从 "error: use of deleted function" 收到此错误。我正在尝试使用 smart_ptrs 的复杂性来研究 C++ 的基础知识,感谢任何有关此错误的指示。
所有参与函数(错误中)如下所示:
class Matrix{
private:
std::unique_ptr<T[]> array=std::make_unique<T[]>(M*N);
public:
Matrix();
Matrix( const Matrix& other );
inline Matrix operator+( const Matrix& other ) const;
const Matrix& operator=( const Matrix< M, N, T >& other );
}
Matrix::Matrix(const Matrix& other)
{
*this = other;
}
inline Matrix
Matrix::operator+( const Matrix& other ) const
{
Matrix result(*this);
result += other;
return result;
}
const Matrix&
Matrix::operator=( const Matrix& other )
{
if(this != &other)
this->array = std::move(other.array);
return *this;
}
错误报告地点:
error: use of deleted function ‘matlib::Matrix<2ul, 2ul, double>::Matrix(const matlib::Matrix<2ul, 2ul, double>&)’
Matrix< M, N, T > result(*this);
error: use of deleted function ‘std::unique_ptr<_Tp [], _Dp>& std::unique_ptr<_Tp [], _Dp>::operator=(const std::unique_ptr<_Tp [], _Dp>&) [with _Tp = double; _Dp = std::default_delete<double []>]’
this->array = std::move(other.array);
提前致谢。
一般来说,您的大部分错误都源于std::unique_ptr
没有复制构造函数。这样它就可以有效地管理指针内部的范围。解决此问题的一种方法是创建一个新的 std::unique_ptr
,然后复制所有单独的值。
我已经为你写了一个例子class,也许它能帮到你。
#include <memory>
template <class T>
class Matrix
{
private:
std::unique_ptr<T[]> data = nullptr;
size_t height, width;
public:
Matrix(size_t h, size_t w)
: height(h), width(w)
{
if(h*w == 0) return;
data = std::make_unique<T[]>(h*w);
}
Matrix(const Matrix& other)
{
height = other.height;
width = other.width;
data = std::make_unique<T[]>(height * width);
for(size_t i = 0; i < height; i++)
{
for(size_t j = 0; j < width; j++)
{
(*this)(j, i) = other(j,i);
}
}
}
Matrix operator=( const Matrix& other )
{
if(this == &other)
{
return *this;
}
height = other.height;
width = other.width;
data.reset(std::make_unique<T[]>(other.height * other.width));
for(size_t i = 0; i < height; i++)
{
for(size_t j = 0; j < width; j++)
{
(*this)(j, i) = other(j,i);
}
}
return *this;
}
T & operator()(size_t x, size_t y)
{
//If data is nullptr then this is undefined behaviour.
//Consider adding a throw or assert here
return data[y * height + x];
}
T operator()(size_t x, size_t y) const
{
//If data is nullptr then this is undefined behaviour.
//Consider adding a throw or assert here
return data[y * height + x];
}
size_t getHeight() const
{
return height;
}
size_t getWidth() const
{
return width;
}
};
作为最后的声明,如果您想为数学目的创建矩阵,我建议您出于性能原因为它们提供静态大小。像这样向 class 添加数学运算符涉及维度不匹配情况的额外逻辑。由于它们的类型,静态大小的矩阵将自行解决这个问题。您仍然可以这样做,但要警惕任何边缘情况。
伪class执行矩阵加法,使用std::unique_ptr作为数据成员来表示元素。 M 行,N 列。 Class 是类型 M、N 的模板。我正在尝试执行矩阵加法 (C = A+B) 并不断从 "error: use of deleted function" 收到此错误。我正在尝试使用 smart_ptrs 的复杂性来研究 C++ 的基础知识,感谢任何有关此错误的指示。
所有参与函数(错误中)如下所示:
class Matrix{
private:
std::unique_ptr<T[]> array=std::make_unique<T[]>(M*N);
public:
Matrix();
Matrix( const Matrix& other );
inline Matrix operator+( const Matrix& other ) const;
const Matrix& operator=( const Matrix< M, N, T >& other );
}
Matrix::Matrix(const Matrix& other)
{
*this = other;
}
inline Matrix
Matrix::operator+( const Matrix& other ) const
{
Matrix result(*this);
result += other;
return result;
}
const Matrix&
Matrix::operator=( const Matrix& other )
{
if(this != &other)
this->array = std::move(other.array);
return *this;
}
错误报告地点:
error: use of deleted function ‘matlib::Matrix<2ul, 2ul, double>::Matrix(const matlib::Matrix<2ul, 2ul, double>&)’
Matrix< M, N, T > result(*this);
error: use of deleted function ‘std::unique_ptr<_Tp [], _Dp>& std::unique_ptr<_Tp [], _Dp>::operator=(const std::unique_ptr<_Tp [], _Dp>&) [with _Tp = double; _Dp = std::default_delete<double []>]’
this->array = std::move(other.array);
提前致谢。
一般来说,您的大部分错误都源于std::unique_ptr
没有复制构造函数。这样它就可以有效地管理指针内部的范围。解决此问题的一种方法是创建一个新的 std::unique_ptr
,然后复制所有单独的值。
我已经为你写了一个例子class,也许它能帮到你。
#include <memory>
template <class T>
class Matrix
{
private:
std::unique_ptr<T[]> data = nullptr;
size_t height, width;
public:
Matrix(size_t h, size_t w)
: height(h), width(w)
{
if(h*w == 0) return;
data = std::make_unique<T[]>(h*w);
}
Matrix(const Matrix& other)
{
height = other.height;
width = other.width;
data = std::make_unique<T[]>(height * width);
for(size_t i = 0; i < height; i++)
{
for(size_t j = 0; j < width; j++)
{
(*this)(j, i) = other(j,i);
}
}
}
Matrix operator=( const Matrix& other )
{
if(this == &other)
{
return *this;
}
height = other.height;
width = other.width;
data.reset(std::make_unique<T[]>(other.height * other.width));
for(size_t i = 0; i < height; i++)
{
for(size_t j = 0; j < width; j++)
{
(*this)(j, i) = other(j,i);
}
}
return *this;
}
T & operator()(size_t x, size_t y)
{
//If data is nullptr then this is undefined behaviour.
//Consider adding a throw or assert here
return data[y * height + x];
}
T operator()(size_t x, size_t y) const
{
//If data is nullptr then this is undefined behaviour.
//Consider adding a throw or assert here
return data[y * height + x];
}
size_t getHeight() const
{
return height;
}
size_t getWidth() const
{
return width;
}
};
作为最后的声明,如果您想为数学目的创建矩阵,我建议您出于性能原因为它们提供静态大小。像这样向 class 添加数学运算符涉及维度不匹配情况的额外逻辑。由于它们的类型,静态大小的矩阵将自行解决这个问题。您仍然可以这样做,但要警惕任何边缘情况。