构造函数不能从抽象类创建对象
Constructor can't make object from abstract class
我正在编程一个棋盘,我有一个基类chesspie(在我的语言中是schaakstuk),所有的棋子,如国王,王后,都是从这个基类派生的。
现在我想创建一个对象并填充一个对象数组来开始游戏。Visual studio在这一行给了我一些错误:
bord[1][kolom] = new Schaakstuk(Schaakstuk::WIT);
bord[6][kolom] = new Pion(Schaakstuk::ZWART);
是不可能从抽象类创建的。我没有看到错误,首先我认为这是我在派生类中使用纯虚函数,但事实并非如此,我只在基类中使用纯虚函数。
构造函数for( int kolom = 0; kolom < SIZE; kolom++ )
{
bord[1][kolom] = new Pion(Schaakstuk::WIT);
bord[6][kolom] = new Pion(Schaakstuk::ZWART);
}
Pion.h
#include "Schaakstuk.h"
#include "Exceptions.h"
#ifndef PION
#define PION
class Pion: public Schaakstuk
{
public:
Pion(void);
~Pion(void);
bool ZetIsLegaal( int rij1, int kolom1, int rij2, int kolom2 ) const;
void PrintStuk( void ) const;
void GeefCor( int tabel [8][2], int rij, int kolom, int rij1, int kolom1) const;
bool IsPion( void ) const { return true; };
private:
bool ControleerZet( int rij1, int kolom1, int rij2, int kolom2 ) const;
};
#endif
Schaakstuk.h
#ifndef SCHAAKSTUK
#define SCHAAKSTUK
static const int SIZE1 = 8;
class Schaakstuk
{
public:
enum kleurType { WIT, ZWART };
Schaakstuk(kleurType kleur = WIT)
{
this->kleur = kleur;
};
virtual bool ZetIsLegaal( int rij1, int kolom1, int rij2, int kolom2 ) = 0;
virtual void PrintStuk( void ) = 0;
virtual void GeefCor( int tabel [8][2], int rij, int kolom, int rij1, int kolom1) = 0;
kleurType GeefKleur( void ) const { return kleur; };
virtual bool IsPion( void ) = 0;
protected:
bool static NietOutOfBounds( int rij, int kolom );
private:
kleurType kleur;
};
#endif
是我存放代码文件的dropbox。有人能帮帮我吗?
错误如下:
http://pastebin.com/82j08rry,这里是完整的代码
http://ideone.com/sWjxS如果错误是"Can't instantiate abstract class",那么下一行应该告诉你哪个方法是抽象的。
最有可能的是你在基类中声明了一个纯虚函数,但没有覆盖它(或者正确地覆盖它;
首先检查确保您在Schaakstuk
和Pion
中有覆盖,然后检查确保您根本没有更改签名。这可以是不同的const/volatile限定条件,或者不同的方法参数。
"抽象"这个词是泄露的。你需要从一个具体的类中创建——也就是说,编译器需要知道这个对象的所有细节。
相关文章:
- 无法创建抽象类的实例
- 使用基类指针创建对象时,缺少派生类析构函数
- 具有包含其他对象的类的对象创建顺序
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- C++,创建新对象时类的对象更改
- C ++如何在原始抽象类中创建一个函数,该函数接受派生类的输入
- 如何创建shared_ptr抽象类的容器
- 使用抽象类的类和实现此抽象类的创建对象
- 创建一个抽象类类型的集合,shared_ptr的抽象类向量
- 如何在 C++ 中创建抽象类的对象或数据结构?
- 创建抽象类 C++ 的对象
- 如何向创建对象的类发送消息
- 在CPP中创建一个抽象类而不是基类会给我带来什么好处
- 在尝试创建SceneNode时分配抽象类类型的对象
- c++,在抽象类中创建匿名子类对象
- 在c++ /QT中创建抽象类对象的QList
- 构造函数不能从抽象类创建对象
- 如何创建抽象类的无名派生类的对象
- 在c++中创建派生抽象类的实例
- 抽象类在创建派生类的对象时出错