C++问题:无法分配抽象类型的对象,但为什么

C++ issue : Cannot allocate an object of abstract type, but why?

本文关键字:类型 对象 为什么 抽象类 抽象 问题 分配 C++      更新时间:2023-10-16

这是我C++程序的一些类。

ElementTerrain.h:

#ifndef ELEMENTTERRAIN_H_
#define ELEMENTTERRAIN_H_
#include <iostream>
#include <string>
using namespace std;
class ElementTerrain {
public:
    virtual ~ElementTerrain(){}
    virtual string getElement() const = 0;
    virtual string getType() const = 0;
    virtual int getStock() const = 0;
};
#endif /* ELEMENTTERRAIN_H_ */

我的.h:

#ifndef MINE_H_
#define MINE_H_
#include "ElementTerrain.h"
using namespace std;
class Mine : public ElementTerrain{
public:
    Mine();
    Mine(bool, bool, int);
    Mine(const Mine &);
    virtual ~Mine();
    string getElement(){
            return "Mine";
        }
    string getType(){
        if(this->ur == true){
            return "UR";}
        if(this->plu == true){
            return "PLU";}
        return "None";
    }
    int getStock() {
        return stock;
    }
    void setStock(int stock) {
        this->stock = stock;
    }
    bool isUr() {
        return ur;
    }
    bool isPlu() {
        return plu;
    }
private :
    bool ur;
    bool plu;
    int stock;
};
#endif /* MINE_H_ */

我的.cpp:

#include "Mine.h"
using namespace std;
Mine::Mine() {
    this->ur = false;
    this->plu = false;
    this->stock = 0;
}
Mine::Mine(bool ur, bool plu, int stock){
    this->ur=ur;
    this->plu=plu;
    this->stock = stock;
}
Mine::Mine(const Mine &m){
    this->ur=m.ur;
    this->plu=m.plu;
    this->stock = m.stock;
}
Mine::~Mine() {
    // TODO Auto-generated destructor stub
}

这是我遇到错误的文件:

#include "ElementRobot.h"
#include "Mine.h"
#include <iostream>
using namespace std;
bool ElementRobot::RecolteMine(Terrain& t, ElementRobot& r) {
    if(t.plateau[x][y]->getElem() != NULL){
            if(t.plateau[x][y]->getElem()->getElement() == "Mine"){
                Mine *m = new Mine();
                if(m.getType() == r.getType() && m.getStock()>0 && r.stock<r.cap){
                    m.setStock(m.getStock()-1);
                    t.plateau[x][y]->setElem((Mine) t.plateau[x][y]->getElem());
                    return true;
                }
                if(m.getType() == r.getType() && m.getStock()==0){
                    cout << "Les stocks de cette mine sont épuisés.n" << endl;
                }
                if(r.stock==r.cap){
                    cout << "Votre robot ne peut pas porter plus de minerai.n" << endl;
                }
                if(m.getType() != r.getType()){
                    cout << "Ce robot n'est pas adapté à cette mine.n" << endl;
                }
            }}
            return false;
}

我想使用复制构造函数创建一个 Mine 类型的对象(这里我只尝试使用默认构造函数),但它说我无法分配抽象类型 Mine 的对象,即使在我的类中Mine没有纯虚拟方法。我是初学者C++我不明白我的错误。我在互联网上也找不到任何东西。

Mine成员函数的签名与基类的签名不匹配(缺少const限定符)。因此,您没有覆盖它们,而是重载了它们,Mine仍然是抽象的,不可实例化。

这实际上就像有这个:

class Mine {
public:
    // this has to be implemented by deriving classes
    virtual string getElement() const = 0;
    // this is a separate overload
    string getElement() { ... };
};

解决方案:修复签名:

string getElement() const { ... }
//                  ^^^^^

等等...

C++11 的 override 关键字将对您有很大帮助 - 它会指出没有名为 getElement 的非常量限定虚拟成员函数可以覆盖。

您忘记了函数声明中的const

看这里:

class ElementTerrain {
public:
    virtual ~ElementTerrain(){}
    virtual string getElement() const = 0;   //  <- notice 'const'
    virtual string getType() const = 0;   //  <- notice 'const'
    virtual int getStock() const = 0;   //  <- notice 'const'
};

因此const关键字添加到getElementgetTypegetStock函数中。

class Mine : public ElementTerrain{
public:
    Mine();
    Mine(bool, bool, int);
    Mine(const Mine &);
    virtual ~Mine();
    string getElement() const{    // notice 'const' is added here..
            return "Mine";
        }
    string getType() const{   // notice 'const' is added here..
        if(this->ur == true){
            return "UR";}
        if(this->plu == true){
            return "PLU";}
        return "None";
    }
    int getStock() const{   // notice 'const' is added here..
        return stock;
    }
    void setStock(int stock) {
        this->stock = stock;
    }
    bool isUr() {
        return ur;
    }
    bool isPlu() {
        return plu;
    }
private :
    bool ur;
    bool plu;
    int stock;
};
另外,如果您想

永远摆脱此错误,如果您想覆盖这样的函数,我建议您在函数声明后添加覆盖 C++11 关键字:

int getStock() const override{
    return stock;
}

在这种情况下,您的C++编译器将检查此类虚拟函数是否存在于任何超类中,如果不存在,则会引发编译错误。