我收到此编译错误:

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++ - 无法使用继承的纯虚拟方法分配抽象类型的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36672716/

10-12 20:33