gpt4 book ai didi

c++ - 错误 C2248 : 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:45:57 25 4
gpt4 key购买 nike

目前我正在尝试使用 std::unique_ptr,但我在 Visual Studio 2012 中遇到编译器错误。

class A
{
private:
unique_ptr<A> other;
public:
unique_ptr<A> getOther()
{
return other;
}
};

错误是:

error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
with
[
_Ty=A
]
c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(1447) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
with
[
_Ty=A
]

最佳答案

我相信std::unique_ptr可以是返回类型,如果返回的值是本地范围的(即,它一离开函数就被销毁)。在你的情况下不是,因为你要返回 unique_ptr<A> other ,在类有效时具有生命周期。

关于c++ - 错误 C2248 : 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21871247/

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