vendredi 4 août 2017

unique_ptr linked list insert - no match for operator=

I am trying new unique_ptr approach in cpp and here's my linked list till now -

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

However on compiling this, the following error is given -

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’

I'm having difficulty to grok what exactly is the error :(

Aucun commentaire:

Enregistrer un commentaire