gpt4 book ai didi

c++ - unique_ptr 链表插入 - 与 operator= 不匹配

转载 作者:行者123 更新时间:2023-11-30 02:24:19 25 4
gpt4 key购买 nike

我是trying cpp 中的新 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’

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

最佳答案

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

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

而不是:

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

关于c++ - unique_ptr 链表插入 - 与 operator= 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45508113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com