C++问题:无法分配抽象类型的对象,但为什么
C++ issue : Cannot allocate an object of abstract type, but why?
这是我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
关键字添加到getElement
、getType
和getStock
函数中。
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++编译器将检查此类虚拟函数是否存在于任何超类中,如果不存在,则会引发编译错误。
相关文章:
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- C++ 多态模板类,模板类型对象的实例化
- 如何分配适合容纳 T 类型对象的缓冲区(可能过度对齐、可能有运算符 new 等)
- 如何构造一个 std::variant 类型对象,其自身 Templated 和构造函数转发参数
- C++:初始化指向具有不同类型对象的指针数组
- 在类 C++ 中构造的模板类型对象
- 如何将颁发者名称设置为x509_req类型对象
- 将任何数据类型/对象作为参数传递以确定其大小
- 将引用类型对象的数组从C#封送到C++
- <T> 从类型对象创建类型<T1>对象的构造函数
- C++是否有现有的方法来检查对象是否是派生类型对象
- C 模板:创建与现有对象相同的类型对象
- 链接列表中的虚函数 - 多种返回类型(对象)
- 没有适当操作员()的类型对象的呼叫
- 制作垫子类型对象的数组.输出窗口显示同一帧
- 抽象类型对象的分配
- 在std::list中就地创建自定义类型对象
- 接受泛型列表类型对象的参数
- C++-将模板类型对象强制转换为特定的数据类型
- 试图在C++中打印存储在类型对象数组中的信息