gpt4 book ai didi

c++ - 不能使用继承、纯虚方法分配抽象类型的对象

转载 作者:太空宇宙 更新时间:2023-11-04 16:05:16 24 4
gpt4 key购买 nike

我遇到这个编译错误:

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

知道发生了什么事吗?

最佳答案

您只将四个重写之一作为常量函数。其他三个与基类中的签名不匹配,因为它们缺少 const

override 说明符 (C++11) 帮助编译器提供更多信息性错误消息并避免遗漏覆盖。参见 http://en.cppreference.com/w/cpp/language/override了解更多信息和示例。

关于c++ - 不能使用继承、纯虚方法分配抽象类型的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36672716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com