繁体   English   中英

C ++将已删除函数与unique_ptr,make_unique一起使用

[英]C++ use of deleted function with unique_ptr, make_unique

使用std :: unique_ptr作为数据成员表示元素的伪类,执行矩阵添加。 M行,N列。 类是针对类型M,N进行模板化的。我正在尝试执行矩阵加法(C = A + B),并不断从“错误:使用删除的函数”中得到此错误。 我正在尝试通过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 ,然后复制所有单个值。

我为您编写了一个示例类,也许可以帮上忙。

#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;
    }
};

最后,如果您要做的是出于数学目的创建矩阵,出于性能原因,建议您为它们提供静态大小。 在这样的类中添加数学运算符会涉及尺寸不匹配的情况下的附加逻辑。 静态大小的矩阵将根据其类型自行解决。 您仍然可以这样做,但是要警惕任何极端情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM