错误C2248:'std :: unique_ptr< _Ty> :: unique_ptr':无法访问在'std :: unique_ptr< _Ty> [英] error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'

查看:703
本文介绍了错误C2248:'std :: unique_ptr< _Ty> :: unique_ptr':无法访问在'std :: unique_ptr< _Ty>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently I am trying to use a std::unique_ptr, but getting compiler error in 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>'
1>          with
1>          [
1>              _Ty=A
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(1447) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
1>          with
1>          [
1>              _Ty=A
1>          ]


推荐答案

我相信unique_ptr可以是返回类型,如果返回的值是局部范围的(即,一旦它出来的函数就被销毁)....在你的case它的不,因为ur返回unique_ptr<>其他,有生命时间,直到类有效。

I believe unique_ptr can be return type, if the value that is returned is of local scope.(i.e, it gets destroyed as soon as it gets out of the function) .... in your case its not, since u r returning unique_ptr <> other, which has life time till the class is valid.

这篇关于错误C2248:'std :: unique_ptr&lt; _Ty&gt; :: unique_ptr':无法访问在'std :: unique_ptr&lt; _Ty&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆