所以在我的代碼中,我試圖將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;
}
派生類代碼:
.h 檔案
class Sheep : Organism
{
Sheep( coordinates organism_pos, World* world);
};
.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;
}
uj5u.com熱心網友回復:
類似的默認成員的知名度如何class的private,繼承也是private,除非另有說明。您需要從Organism publicly繼承,以便std::unique_ptr能夠查看和執行您期望的轉換。
class Sheep : public Organism {
public:
Sheep( coordinates organism_pos, World* world);
}
您的建構式也需要public這樣std::make_unique才能看到和使用它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/314613.html
上一篇:如何覆寫從javascript中的模塊回傳的類中的函式
下一篇:如何將物件陣列傳遞給函式?
