简体   繁体   中英

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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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