2016-11-25 56 views
0

我一直试图解决这个问题好几个小时,因为我不能得到一个指针来删除自己,所以我创建了一个基于文本的程序进行调试。为什么这不起作用?如何添加一个auto_ptr到一个矢量

#include <iostream> 
#include <memory> 
#include <vector> 

using namespace std; 

class A {}; 

int main() { 
    vector<A> as; 
    std::auto_ptr<A> a(new A); 
    as.push_back(std::move(a)); 
    return 0; 
} 
+0

现在就做你正在做的事情,但是使用unique_ptr而不是auto_ptr。 – immibis

+3

不要使用'std :: auto_ptr'使用'std :: unique_ptr' – Galik

+0

@immibis它说“没有匹配函数调用'std :: vector :: push_back(std :: remove_reference &> :: type)'“ – Merle

回答

1

考虑载体含有A对象,你要as.push_back(*a)。您不需要std::move,临时*a已经可以移动。