【问题标题】:unique_ptr linked list insert - no match for operator=unique_ptr 链表插入 - operator= 不匹配
【发布时间】:2018-01-12 11:16:24
【问题描述】:

我是 cpp 中的 tryingunique_ptr 方法,这是我到目前为止的链接列表 -

#include <memory>

class LL {
        private: int data; std::unique_ptr<LL> next;

        public:
        LL(const int value) : data(value), next(nullptr) {};
        void insert(const int value) {
            LL *current = this;
            while(current->next.get() != nullptr) {
                current = current->next.get();
            }
            current->next = std::make_unique<int>(value);
        }
};

int main() {
    LL list(2);
    list.insert(4);
}

但是在编译时,会出现以下错误 -

prog.cpp: In member function ‘void LL::insert(int)’:
prog.cpp:13:56: error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<LL>’ and ‘std::_MakeUniq<int>::__single_object {aka std::unique_ptr<int, std::default_delete<int> >}’)
             current->next = std::make_unique<int>(value);
                                                        ^
In file included from /usr/include/c++/6/memory:81:0,
                 from prog.cpp:1:
/usr/include/c++/6/bits/unique_ptr.h:252:7: note: candidate: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = LL; _Dp = std::default_delete<LL>]
       operator=(unique_ptr&& __u) noexcept
       ^~~~~~~~
/usr/include/c++/6/bits/unique_ptr.h:252:7: note:   no known conversion for argument 1 from ‘std::_MakeUniq<int>::__single_object {aka std::unique_ptr<int, std::default_delete<int> >}’ to ‘std::unique_ptr<LL>&&’
/usr/include/c++/6/bits/unique_ptr.h:272:2: note: candidate: template<class _Up, class _Ep> typename std::enable_if<std::__and_<std::__and_<std::is_convertible<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Tp, _Dp>::_Pointer::type>, std::__not_<std::is_array<_Up> >, std::__or_<std::__and_<std::is_reference<_Dp>, std::is_same<_T2, _U2> >, std::__and_<std::__not_<std::is_reference<_Dp> >, std::is_convertible<_Ep, _Dp> > > >, std::is_assignable<_T2&, _U2&&> >::value, std::unique_ptr<_Tp, _Dp>&>::type std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = _Up; _Ep = _Ep; _Tp = LL; _Dp = std::default_delete<LL>]
  operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
  ^~~~~~~~
/usr/include/c++/6/bits/unique_ptr.h:272:2: note:   template argument deduction/substitution failed:
/usr/include/c++/6/bits/unique_ptr.h: In substitution of ‘template<class _Up, class _Ep> typename std::enable_if<std::__and_<std::__and_<std::is_convertible<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Tp, _Dp>::_Pointer::type>, std::__not_<std::is_array<_Up> >, std::__or_<std::__and_<std::is_reference<_Dp>, std::is_same<_T2, _U2> >, std::__and_<std::__not_<std::is_reference<_Dp> >, std::is_convertible<_Ep, _Dp> > > >, std::is_assignable<_T2&, _U2&&> >::value, std::unique_ptr<_Tp, _Dp>&>::type std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = int; _Ep = std::default_delete<int>]’:
prog.cpp:13:56:   required from here
/usr/include/c++/6/bits/unique_ptr.h:272:2: error: no type named ‘type’ in ‘struct std::enable_if<false, std::unique_ptr<LL>&>’
/usr/include/c++/6/bits/unique_ptr.h:281:7: note: candidate: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::nullptr_t) [with _Tp = LL; _Dp = std::default_delete<LL>; std::nullptr_t = std::nullptr_t]
       operator=(nullptr_t) noexcept
       ^~~~~~~~
/usr/include/c++/6/bits/unique_ptr.h:281:7: note:   no known conversion for argument 1 from ‘std::_MakeUniq<int>::__single_object {aka std::unique_ptr<int, std::default_delete<int> >}’ to ‘std::nullptr_t’

我很难理解到底是什么错误:(

【问题讨论】:

    标签: c++ c++11 pointers linked-list c++14


    【解决方案1】:

    因为您正在创建一个指向您的类 LL 的对象的唯一指针,所以它应该是:

    current->next = std::make_unique<LL>(value);
    

    而不是:

    current->next = std::make_unique<int>(value);
    

    【讨论】:

      【解决方案2】:
      current->next = std::make_unique<int>(value);
      

      这里,左边是std::unique_ptr&lt;LL&gt;类型,右边是std::unique_ptr&lt;int&gt;类型,编译器不知道如何将一个分配给另一个。

      看来你是想说

      current->next = std::make_unique<LL>(value);
      

      改为。

      【讨论】:

        猜你喜欢
        • 2011-03-13
        • 2020-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-09
        • 1970-01-01
        相关资源
        最近更新 更多