简体   繁体   English

没有匹配的函数来调用..constructor

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

I am trying to compile a code that involves inheritance. 我正在尝试编译涉及继承的代码。

#include "MapEntityClass.h"

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

  protected:
};

The parent class is MapEntityClass, which does not have a default constructor, but has a value constructor. 父类是MapEntityClass,它没有默认构造函数,但具有值构造函数。 When I compile, I get the following error: 编译时,出现以下错误:

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

Any idea what is wrong? 知道有什么问题吗?

In inheritance, the subclass need not have a constructor only if parent class doesn't have a constructor or only default constructor. 在继承中,仅当父类没有构造函数或只有默认构造函数时,子类才不需要构造函数。

In any case, if parent class happens to have a parameterized constructor, the subclass should have a parameterized constructor which should invoke the parent class constructor. 无论如何,如果父类碰巧具有参数化的构造函数,则子类应具有参数化的构造函数,该参数化构造函数应调用父类的构造函数。

Example: 例:

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