我收到此编译错误:
main.cpp:34:74: error: cannot allocate an object of abstract type ‘HouseClass’
mapEntVec.push_back(new HouseClass(PixelLocationClass(120, 220), 100000));
^
In file included from main.cpp:2:0:
HouseClass.h:17:7: note: because the following virtual functions are pure within ‘HouseClass’:
class HouseClass:public RectangularEntityClass
^
In file included from HouseClass.h:8:0,
from main.cpp:2:
RectangularEntityClass.h:18:20: note: virtual std::string RectangularEntityClass::getType() const
virtual string getType() const = 0;
^
RectangularEntityClass.h:19:17: note: virtual int RectangularEntityClass::getNumRows() const
virtual int getNumRows() const = 0;
^
RectangularEntityClass.h:20:17: note: virtual int RectangularEntityClass::getNumCols() const
virtual int getNumCols() const = 0;
以下是相关代码:
#ifndef _HOUSECLASS_H_
#define _HOUSECLASS_H_
//Programmer:
//Date: April 2016
//Purpose:
#include "RectangularEntityClass.h"
#include "ColorClass.h"
#include <string>
//Some house-specific global constants..
const ColorClass HOUSE_COLOR = ColorClass(255, 0, 0);
const int PIXEL_TO_DOLLAR = 15000;
const string HOUSE_TYPE_STR = "House";
class HouseClass:public RectangularEntityClass
{
private:
int dollarValue;
public:
//The only ctor that is available - houses are described via a location
//are to be positioned within a map and a dollar amount the house costs
//the color of the house is maintained as a global constant specific to
//houses
HouseClass(const PixelLocationClass &inPixLoc, int inDollarValue)
{
//need to call rectangular class constructor?
dollarValue = inDollarValue;
}
string toString() const;
string getType();
int getNumRows();
int getNumCols();
};
#endif
#include <sstream>
#include <string>
#include "HouseClass.h"
using namespace std;
//Programmer:
//Date: April 2016
//Purpose:
string HouseClass::toString() const
{
ostringstream oss;
oss.clear();
oss.str("");
oss << SCHOOL_COLOR << " Value: " << dollarValue << " " <<
MapEntityClass::toString();
return(oss.str());
}
string HouseClass::getType()
{
return(SCHOOL_COLOR);
}
int HouseClass::getNumRows()
{
numRows = dollarValue/PIXEL_TO_DOLLAR + 1;
return numRows;
}
int HouseClass::getNumCols()
{
numCols = dollarValue/PIXEL_TO_DOLLAR + 1;
return numCols;
}
#ifndef _RECTANGULARENTITYCLASS_H_
#define _RECTANGULARENTITYCLASS_H_
//Programmer:
//Date: April 2016
//Purpose:
#include "MapEntityClass.h"
class RectangularEntityClass:public MapEntityClass
{
public:
RectangularEntityClass():MapEntityClass(location, color) {}
protected:
void drawOnMap(MapClass *mapObj) const;
virtual string toString() const = 0;
virtual string getType() const = 0;
virtual int getNumRows() const = 0;
virtual int getNumCols() const = 0;
};
#endif
知道发生了什么事吗?
答案 0 :(得分:5)
您只将四个覆盖中的一个作为const函数。其他三个不匹配基类中的签名,因为它们缺少const
。
override
说明符(C ++ 11)帮助编译器提供更多信息性错误消息,避免错过覆盖。有关详细信息和示例,请参阅http://en.cppreference.com/w/cpp/language/override。
答案 1 :(得分:0)
您无法创建纯虚拟数据类型的对象。这是c ++ 101。 您可以从该数据类型继承,并分配给该类型。 您还可以在函数中使用纯虚拟数据类型,然后发送到该函数。