构造函数不能从抽象类创建对象

Constructor can't make object from abstract class

本文关键字:创建对象 抽象类 不能 构造函数      更新时间:2023-10-16

我正在编程一个棋盘,我有一个基类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",那么下一行应该告诉你哪个方法是抽象的。

最有可能的是你在基类中声明了一个纯虚函数,但没有覆盖它(或者正确地覆盖它;

首先检查确保您在SchaakstukPion中有覆盖,然后检查确保您根本没有更改签名。这可以是不同的const/volatile限定条件,或者不同的方法参数。

"抽象"这个词是泄露的。你需要从一个具体的类中创建——也就是说,编译器需要知道这个对象的所有细节。