【发布时间】:2014-04-02 22:41:54
【问题描述】:
对于学校作业,我正在尝试使用指向 Employee 对象的唯一指针向量来访问 Employee 数据,但无法找出语法/编译器错误。谁能告诉我我做错了什么?必须以这种方式使用智能指针向量。
这是适用的代码:
// Create an Employee
Employee EmpRec;
// Assign value to a uniqueptr
unique_ptr<Employee> TempEmp;
*TempEmp = EmpRec;
// Create a vector of unique_ptr<Employee>
vector<unique_ptr<Employee>> EmpVect;
// Push the TempEmp pointer onto the vector
EmpVect.push_back(TempEmp);
// Iterate through vector, calling display function
//that prints the values of various data inside the Employee object
for (size_t i = 0; i < EmpVect.size(); ++i){
(EmpVect[i])->display(cout);
}
这就是我的 Display 函数的定义方式:
void display(std::ostream& cout) const{
// print data members using cout <<
}
尝试编译时,出现以下错误:
d:\microsoft visual studio 12.0\vc\include\xmemory0(593): error C2280: 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_ty>> & )' : 试图引用一个已删除的函数
【问题讨论】:
标签: c++ pointers vector ostream