2014-02-19 77 views
2

目前,我试图用一个std::unique_ptr,但我在Visual得到一个编译器错误工作室2012年错误C2248:“的std ::的unique_ptr <_Ty> ::的unique_ptr”:不能访问类中声明私有成员“的std ::的unique_ptr <_Ty>”

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 
] 
+3

你不能复制unique_ptr,因为......它们是唯一的。 (提示:您将按照值返回指针,这会生成副本) – Borgleader

+1

如果您需要多个副本,为什么使用'unique_ptr'? –

+0

好的...我对这个错误感到困惑。现在我得到了确切的问题。 – arunkn

回答

0

相信std::unique_ptr可以返回类型,如果返回的值是本地范围内(即,它就会尽快销毁它离开了func荐)。在你的情况下,它不是,因为你正在返回unique_ptr<A> other,这有生命时间,而类是有效的。

相关问题