簡體   English   中英

沒有匹配的函數來調用..constructor

[英]no matching function for call to ..constructor

我正在嘗試編譯涉及繼承的代碼。

#include "MapEntityClass.h"

class RectangularEntityClass:public MapEntityClass
{
  public:
    void drawOnMap(MapClass *mapObj) const;

  protected:
};

父類是MapEntityClass,它沒有默認構造函數,但具有值構造函數。 編譯時,出現以下錯誤:

RectangularEntityClass.h: In constructor ‘RectangularEntityClass::RectangularEntityClass()’:
RectangularEntityClass.h:12:7: error: no matching function for call to ‘MapEntityClass::MapEntityClass()’
   class RectangularEntityClass:public MapEntityClass
   ^
RectangularEntityClass.h:12:7: note: candidates are:
In file included from main.cpp:1:0:
MapEntityClass.h:32:5: note: MapEntityClass::MapEntityClass(const PixelLocationClass&, const ColorClass&)
     MapEntityClass(
     ^
MapEntityClass.h:32:5: note:   candidate expects 2 arguments, 0 provided

知道有什么問題嗎?

在繼承中,僅當父類沒有構造函數或只有默認構造函數時,子類才不需要構造函數。

無論如何,如果父類碰巧具有參數化的構造函數,則子類應具有參數化的構造函數,該參數化構造函數應調用父類的構造函數。

例:

class A {
    int aVal;
    public:
        A(int);
};

A::A(int aVal)
{
    this->aVal = aVal;
}

class B : public A {
    int bVal;
    public:
        B(int, int)
};

B::B(int aVal, int bVal) : A(aVal)
{
    this->bVal = bVal;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM