I wrote the following code where I am trying to copy the value of a unique_ptr object to a structure.
#include <iostream> #include <memory> using namespace std; struct S { S(int X = 0, int Y = 0):x(X), y(Y){} // S(const S&) {} // S& operator=(const S&) { return *this; } int x; int y; std::unique_ptr<S> ptr; }; int main() { S s; s.ptr = std::unique_ptr<S>(new S(1, 4)); S p = *s.ptr; // Copy the pointer value return 0; }
An error appears in Visual C ++ 2012:
IntelliSense: no suitable custom conversion from "S" to "S" exists
IntelliSense: operator "=" does not match these operands Operand types: std :: unique_ptr> = std :: unique_ptr>
error C2248: 'std :: unique_ptr <_Ty> :: unique_ptr': cannot access the private member declared in the class 'std :: unique_ptr <_Ty>'
If I did not uncomment the lines where I tried to define the copy constructor and = operator. This eliminates compiler errors, but not IntelliSense errors. It compiles regardless of the IntelliSense errors displayed in the error list.
So why can't it use the default functions and compile with them? Am I copying the value correctly? How to define a copy constructor if needed?
source share