gpt4 book ai didi

c++ - 在 C++ 中实现纯虚类的问题

转载 作者:行者123 更新时间:2023-11-27 22:30:02 24 4
gpt4 key购买 nike

我有以下类(class):

#include <string>
#include <stack>
#include <queue>
#include "map.h"

using namespace std;

#ifndef CONTAINER_H_
#define CONTAINER_H_

struct PathContainer {
int x, y;
string path;
};

class Container {
public:
virtual void AddTile(string, FloorTile *) = 0;
virtual void ClearContainer() = 0;
virtual PathContainer *NextTile() = 0;
};

class StackImpl : public Container {
private:
stack<PathContainer> cntr;
public:
StackImpl();
void AddTile(string, NeighborTile *);
void ClearContainer();
PathContainer *NextTile();
};

class QueueImpl : public Container {
private:
queue<PathContainer> cntr;
public:
QueueImpl();
void AddTile(string, NeighborTile *);
void ClearContainer();
PathContainer *NextTile();
};
#endif

当我尝试像这样创建 StackImpl 或 QueueImpl 对象时:

Container *cntr;
cntr = new StackImpl();

Container *cntr;
cntr = new QueueImpl();

编译时出现以下错误:

escape.cpp: In function ‘int main(int, char**)’:escape.cpp:26: error: cannot allocate an object of abstract type ‘StackImpl’container.h:23: note: because the following virtual functions are pure within ‘StackImpl’:container.h:18: note: virtual void Container::AddTile(std::string, FloorTile*)

有什么想法吗?

最佳答案

typeid(NeighborTile *) != typeid(FloorTile *)。签名不同,因此即使 NeighborTile 继承自 FloorTile,它们也不算作“相同”方法。

关于c++ - 在 C++ 中实现纯虚类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3795964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com