gpt4 book ai didi

c++ - shared_ptr 初始化

转载 作者:太空狗 更新时间:2023-10-29 19:53:17 28 4
gpt4 key购买 nike

成员定义为

std::shared_ptr<std::array<std::string, 6> > exit_to;

指向其他人共享的额外数据。当尝试启动指针“exit_to”时。正确的做法是

node_knot.exit_to = std::make_shared<std::array<std::string, 6> >();

但是它在另一个文件中,我想保持指针类型一致,像这样:

node_knot.exit_to = std::make_shared<decltype(*node_knot.exit_to)>();

但不会编译:

 /usr/include/c++/4.6/bits/shared_ptr_base.h:798:54: error: '__p'
declared as a pointer to a reference of type
'std::array<std::basic_string<char>, 6> &'
__shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p)
^ /usr/include/c++/4.6/bits/shared_ptr.h:93:31: note: in instantiation
of template class
'std::__shared_ptr<std::array<std::basic_string<char>, 6> &, 1>'
requested here
class shared_ptr : public __shared_ptr<_Tp>
^ ../node_booker.h:757:20: note: in
instantiation of template class
'std::shared_ptr<std::array<std::basic_string<char>, 6> &>' requested
here
n.exit_to = std::make_shared<decltype(*n.exit_to)>();

我使用的是 Ubuntu 12.10,clang++ 3.2,--std=c++11

最佳答案

您需要从传递给 make_shared 的类型中删除引用。以下应该有效:

node_knot.exit_to = std::make_shared<std::remove_reference<decltype(*node_knot.exit_to)>::type>();

关于c++ - shared_ptr 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15532333/

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