Mic*_*rek 0 c++ inheritance derived-class unique-ptr c++11
因此,在我的代码中,我尝试将unique_ptr对象添加到从derived类到vector基类的对象。我收到此错误:
E0304 没有重载函数的实例“std::vector<_Ty, _Alloc>::push_back [with _Ty=std::unique_ptr<Organism, std::default_delete<Organism>>, _Alloc=std::allocator<std::unique_ptr <Organism, std::default_delete<Organism>>>]" 与参数列表匹配
基类的代码(如果您需要更多,请告诉我,尽量少写代码):
vector<unique_ptr<Organism>> World::generate_organisms(int act_level)
{
vector<unique_ptr<Organism>> organism_list = get_vector();
coordinates sheep_pos(10, 2);
//getting error in next line
organism_list.push_back(make_unique<Sheep>(sheep_pos, *this));
return organism_list;
}
Run Code Online (Sandbox Code Playgroud)
派生类的代码:
.h文件
class Sheep : Organism
{
Sheep( coordinates organism_pos, World* world);
};
Run Code Online (Sandbox Code Playgroud)
.cpp文件
Sheep::Sheep( coordinates organism_pos, World* act_world)
:
Organism(organism_pos, act_world)
{
this->armor = 0;
this->damage = 2;
this->health = 10;
this->vigor = 10;
}
Run Code Online (Sandbox Code Playgroud)
class与的默认成员可见性类似private,private除非另有说明,继承也是如此。您需要从Organism publicly 继承,以便std::unique_ptr能够查看并执行您期望的转换。
class Sheep : public Organism {
public:
Sheep( coordinates organism_pos, World* world);
}
Run Code Online (Sandbox Code Playgroud)
您的构造函数也需要能够public看到std::make_unique和使用它。
| 归档时间: |
|
| 查看次数: |
398 次 |
| 最近记录: |