gpt4 book ai didi

c++ - 从函数返回 unique_ptr 时出现 unique_ptr 所有权错误

转载 作者:太空狗 更新时间:2023-10-29 19:43:59 26 4
gpt4 key购买 nike

<分区>

我是 unique_ptr 的新手。一切都很顺利,直到我遇到一个返回新 unique_ptr 的函数。编译器似乎提示不止一个对象可能拥有 unique_ptr。我不确定如何满足编译器的投诉。感谢您的帮助。

void Bar::Boo()
{
...
// m_Goals is of type std::unique_ptr<Goals>
auto x = m_Goals->Foo(); // error: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
//auto x = std::move( m_Goals->Foo() ); // same error
...
}


const std::unique_ptr<Goals> Goals::Foo()
{
std::unique_ptr<Goals> newGoals( new Goals() );
...
return newGoals;
// also tried "return std::move( newGoals )" based on http://stackoverflow.com/questions/4316727/returning-unique-ptr-from-functions
} // this function compiles

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