2017-08-04 2 views
1

私はCPPでtrying新しいunique_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); 

を言うことを意図したように見えます。

関連する問題