2017-08-04 25 views
1

我在CPP tryingunique_ptr方法,這裏是我的鏈接列表到現在 -的unique_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’ 

我有困難神交究竟是什麼錯誤:(

回答

2

因爲你是一個獨特的指針,你類的對象它應該是:

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

代替:

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

這裏,左手側是std::unique_ptr<LL>類型,並且右手側是std::unique_ptr<int>類型的,並且編譯器不知道如何將一個分配給另一個。

看起來你打算說

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

代替。