我编写了以下代码,我尝试将unique_ptr对象的值复制到结构中.
#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's value return 0; }
它在Visual C 2012中弹出错误:
IntelliSense: no suitable user-defined conversion from “S” to “S”
exists
IntelliSense: no operator “=” matches these operands
operand types are: std::unique_ptr> = std::unique_ptr>
error C2248: ‘std::unique_ptr<_Ty>::unique_ptr’ : cannot access
private member declared in class ‘std::unique_ptr<_Ty>’
除非我取消注释我尝试定义复制构造函数和=运算符的行.
这消除了编译器错误,但没有消除IntelliSense错误.无论错误列表中显示的IntelliSense错误如何,它都会编译.