简体   繁体   English

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

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

A pseudo class performing matrix additon using std::unique_ptr as the data member to represent the elements. 使用std :: unique_ptr作为数据成员表示元素的伪类,执行矩阵添加。 M-rows, N columns. M行,N列。 Class is templated for type, M, N. I am trying to perform a matrix addition (C = A+B) and keep getting this error from "error: use of deleted function". 类是针对类型M,N进行模板化的。我正在尝试执行矩阵加法(C = A + B),并不断从“错误:使用删除的函数”中得到此错误。 I am trying to crunch the fundamentals of C++ with a complication of smart_ptrs, any pointers regarding this error is appreciated. 我正在尝试通过smart_ptrs的复杂性来破坏C ++的基础知识,任何有关此错误的指针都值得赞赏。

All the participating functions (in the error) are shown below: 所有参与的功能(出现错误)如下所示:

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 reporting places: 错误报告地点:

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);

Thanks in advance. 提前致谢。

In general, most of your errors originate from that std::unique_ptr has no copy constructor. 通常,大多数错误都源自该std::unique_ptr没有副本构造函数。 This is so that it can efficiently manage the scope of the pointer inside of it. 这样便可以有效地管理其中的指针范围。 One way around this is to create a new std::unique_ptr , and copy all individual values over. 解决此问题的一种方法是创建一个新的std::unique_ptr ,然后复制所有单个值。

I've written an example class for you, perhaps it can help. 我为您编写了一个示例类,也许可以帮上忙。

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

As a final statement, if what you want to do is to create matrices for mathematical purposes, I suggest you give them static sizes for performance reasons. 最后,如果您要做的是出于数学目的创建矩阵,出于性能原因,建议您为它们提供静态大小。 Adding in mathematical operators to a class like this involves additional logic for the cases where dimensions are mismatched. 在这样的类中添加数学运算符会涉及尺寸不匹配的情况下的附加逻辑。 Statically sized matrices will solve this by themselves due to their typing. 静态大小的矩阵将根据其类型自行解决。 You can still do it like this, but be wary of any edge cases. 您仍然可以这样做,但是要警惕任何极端情况。

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

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