No Matching Function Call to Base class

前端 未结 4 1888
庸人自扰
庸人自扰 2020-12-06 14:41

Here I am trying to make a subclass of the base class, Airplane. In my main code I haven\'t tried to use either constructor yet as I am just trying to make sure I can make t

相关标签:
4条回答
  • 2020-12-06 15:00

    Make sure that if you are calling a base class with a constructor, the name of the constructor is the the same as that of the base class

    0 讨论(0)
  • 2020-12-06 15:03

    When this constructor is called

    Fighter::Fighter(int engines, int seats, string desc)
    {
        fNumEngines = engines;
        fNumSeats = seats;
        rangeSpeed = desc;
    }
    

    then it calls the default base class constructor. However class Airplane does not have the default constructor. It has a constructor with parameters

    Airplane(int, int);
    

    So you need explicitly calll the constructor in the mem-initializer list of the constructor Fighter

    For example

    Fighter::Fighter(int engines, int seats, string desc) : Airplane( engines, seats )
    {
        fNumEngines = engines;
        fNumSeats = seats;
        rangeSpeed = desc;
    }
    

    Also it is not clear why the data members of the base class and the derived class are duplicated.

    0 讨论(0)
  • 2020-12-06 15:13

    The error contains the information that you need. You havent defined a default constructor for class Airplane. When you construct the child class Fighter in this function:

    Fighter(int, int, string);
    

    There will be a call to construct the base class. In your implementation you do not explicitly call the base class constructor:

    Airplane::Airplane(int engines, int seats)

    And you have no default constructor, so what should the compiler do? It complains.

    You wiether need to define a default constructor:

    Airplane::Airplane()
    

    Or call an existing constructor in the constructor for fighter:

    Fighter::Fighter(int engines, int seats, string desc) : Airplane(engines, seats)
    
    0 讨论(0)
  • 2020-12-06 15:16
    Fighter::Fighter(int engines, int seats, string desc)
    {
        fNumEngines = engines;
        fNumSeats = seats;
        rangeSpeed = desc;
    }
    

    is equivalent to:

    Fighter::Fighter(int engines, int seats, string desc) : Airplane()
    {
        fNumEngines = engines;
        fNumSeats = seats;
        rangeSpeed = desc;
    }
    

    The base class object is initialized using the default constructor unless another constructor is used to initialize the base class in the initialization list in the constructor implementation.

    That's why the compiler cannot compile that function.

    You need to:

    1. Add a default constructor to Airplane, or
    2. Use the available constructor of Airplane in the the initialization list.

    Looking at your posted code, option 2 is going to work.

    Fighter::Fighter(int engines, int seats, string desc) :
            Airplane(engines, seats), rangeSpeed(desc)
    {
    }
    

    Suggestion for cleanup

    I don't see why you need the members fNumEngines and fNumSeats in Fighter. The base class already has the members to capture that information. I suggest that you should remove them.

    0 讨论(0)
提交回复
热议问题