繁体   English   中英

无法使用继承的纯虚拟方法分配抽象类型的对象

[英]cannot allocate an object of abstract type using inheritance, pure virtual methods

我收到此编译错误:

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函数。 其他三个与基类中的签名不匹配,因为它们缺少const

override说明符(C ++ 11)可以帮助编译器提供更多有用的错误消息,并避免丢失覆盖。 有关更多信息和示例,请参见http://en.cppreference.com/w/cpp/language/override

您不能创建纯虚拟数据类型的对象。 那是c ++101。您可以从该数据类型继承,并分配给该类型。 您还可以在函数中使用纯虚拟数据类型,然后发送给该函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM