开发者

Problem implementing pure virtual class in C++

I have the following开发者_JS百科 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

When I try creating StackImpl or QueueImpl object like so:

Container *cntr;
cntr = new StackImpl();

or

Container *cntr;
cntr = new QueueImpl();

I get the following error at compile:

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*)

Any ideas?


typeid(NeighborTile *) != typeid(FloorTile *). The signatures differ, so they don't count as "the same" method even if NeighborTile inherits from FloorTile.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜