我需要使用抽象类"有机体"创建一个priority_queue。作为一种存储类型,因为我想要两个"动物"和"植物"在这个队列中(他们来自"有机体")但我不太确定我该怎么做。我已经找到了答案,但遗憾的是找不到一个有效的答案。到目前为止,我的部分代码如下所示:
World.h:
class World
{
protected:
std::string name;
std::priority_queue<Organism, std::vector<Organism>, Compare> organisms;
public:
World(std::string name);
~World();
virtual void AddOrganism(Organism& organisms);
void EndTurn();
void DrawWorld();
};
Compare.h:
class Organism;
class Compare
{
public:
virtual bool operator()(const Organism& O1, const Organism& O2) const;
};
Organism.h:
class World;
class Organism
{
protected:
int strength, initiative, xPos, yPos, age;
World* world;
friend class Compare;
public:
virtual void Move() = 0;
virtual void Action() = 0;
virtual void Collision() = 0;
virtual void DrawMe() = 0;
};
和主要:
int main()
{
SetConsoleTitle(TEXT("xxx"));
World world("World1");
Animals animal;
world.AddOrganism(animal);
system("cls");
return 0;
}
和AddOrganism方法:
void World::AddOrganism(Organism& organism) {
organisms.push(organism);
};
当我尝试编译时,我得到&#34;错误C2259&#39;有机体&#39;:无法实例化抽象类&#34;,有谁知道如何解决这个问题?