【问题标题】:C++ issue : Cannot allocate an object of abstract type, but why?C++ 问题:无法分配抽象类型的对象,但为什么呢?
【发布时间】:2016-05-30 23:21:09
【问题描述】:

这是我的 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 的对象,即使在我的类@ 987654327@没有纯虚方法。我是 C++ 的初学者,我不明白我的错误。我在互联网上也找不到任何东西。

【问题讨论】:

    标签: c++ class object polymorphism abstract


    【解决方案1】:

    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 限定虚成员函数可以覆盖。

    【讨论】:

    • 非常感谢,这非常有用,我没有考虑覆盖和重载之间的区别。我现在会更加小心。
    【解决方案2】:

    您在函数声明中忘记了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;
    
    };
    

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

    int getStock() const override{
        return stock;
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多