arunkn
arunkn

Reputation: 31

error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'

Currently I am trying to use a std::unique_ptr, but I'm getting a compiler error in Visual Studio 2012.

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

And the error is:

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
]

Upvotes: 2

Views: 4981

Answers (1)

vinoth
vinoth

Reputation: 36

I believe std::unique_ptr can be a 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 it's not, since you are returning unique_ptr<A> other, which has life time while the class is valid.

Upvotes: 2

Related Questions