简体   繁体   English

无法初始化std :: unique_ptr

[英]Can't initialize std::unique_ptr

I have written a stack class: 我写了一个堆栈类:

template <class child_t>
class stack {
    const size_t c_init_cap = 12;
    size_t m_size;
    size_t m_capacity;
    std::unique_ptr<child_t[]> m_head;
public:
    /**
     * @brief Default constructor
     */
    stack() : m_size{0}, m_capacity{c_init_cap},
        m_head{std::make_unique<child_t[]>(new child_t[m_capacity])}
    {}
    /**
     * @brief Copy constructor (deleted)
     * @param other Reference to an original object
     */
    stack(const stack &other) = delete;
    /**
     * @brief Move constructor (default)
     * @param other R-value reference to an original object
     */
    stack(stack &&other) = default;
    /**
     * @brief Destructor
     */
    ~stack() {}
    /**
     * @brief Move assigment (default)
     * @param other R-value reference to an original object
     */
    stack& operator =(stack &&other) = default;
    /**
     * @brief Pushes a new value into the stack
     * @param value New value
     */
    void push(child_t value) {
        if (m_size == m_capacity) { increase(); }
        m_head[m_size++] = std::move(value);
    }
    /**
     * @brief Pops a top value from the stack
     */
    void pop() { --m_size; }
    /**
     * @brief Returns a reference to a top value of the stack
     * @return Top value
     */
    child_t &top() { return m_head[m_size - 1]; }
    /**
     * @brief Returns a reference to a top value of the stack
     * @return Top value
     */
    const child_t &top() const { return m_head[m_size - 1]; }
    /**
     * @brief Returns a stack size
     * @return Stack size
     */
    size_t size() const { return m_size; }
    /**
     * @brief Checks if the stack is empty
     * @return Check result
     */
    bool empty() const { return m_size == 0; }
private:
    /**
     * @brief Increases the pre-allocated capacity of the buffer doubly
     */
    void increase() {
        m_capacity <<= 1;
        auto tmp = new child_t[m_capacity];
        std::copy(m_head, m_head + m_size - 1, tmp);
        m_head.reset(tmp);
    }
};

But I'm getting an error on build: 但是我在构建时遇到错误:

/home/Task_3_3/task_3_3/stack.h:20: error: no matching function for call to ‘make_unique<int []>(int*)’
         m_head{std::make_unique<child_t[]>(new child_t[m_capacity])}
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~

What does it mean? 这是什么意思? And how can I fix? 我该如何解决?
PS I'm learning C++ only, so I am a noobie still... PS我只在学习C ++,所以我还是个菜鸟...

The correct use of std::make_unique with an array type is to provide the size of the array as argument. std::make_unique与数组类型的正确用法是提供数组的大小作为参数。

The correct way of initializing m_head would then be m_head{std::make_unique<child_t[]>(m_capacity)} . 初始化m_head的正确方法是m_head{std::make_unique<child_t[]>(m_capacity)}

Probably the most important reason to use make_unique is to avoid using new operator. 使用make_unique的最重要原因可能是避免使用new运算符。 You can read this quesiton for more information: Exception safety and make_unique . 您可以阅读以下问题以获取更多信息: 异常安全性和make_unique

So in your case: 因此,在您的情况下:

std::make_unique<child_t[]>(m_capacity);

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

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