This codes compiles successfully.
#include<iostream>
#include<memory>
using namespace std;
class A{
public:
unique_ptr<A> myval;
A(){ cout<<"Constrcutor of A is called"<<endl; }
~A(){cout<<"Destructor of A is called"<<endl;}
unique_ptr<A> getsomething()
{
unique_ptr<A> myval;
myval.reset(new A);
return myval;
}
};
but when I comment localunique_ptr<A> myval;
compiler throws error.
shared_test.cpp: In member function ‘std::unique_ptr<A> A::getsomething()’:
shared_test.cpp:12:10: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = A; _Dp = std::default_delete<A>]’
return myval;
^
In file included from /usr/include/c++/4.8/memory:81:0,
from shared_test.cpp:2:
/usr/include/c++/4.8/bits/unique_ptr.h:273:7: error: declared here
unique_ptr(const unique_ptr&) = delete;
^
I am not able to understand what this error says. What is it?