Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

c++ - No Matching Function Call to Base class

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 the subclass Fighter to properly work.

The exact error its giving me is

no matching function for call to 'Airplane:Airplane()'

And says it pertains to this line of code in the Fighter.cpp

Fighter::Fighter(int engi, int seat, string description)

Fighter.cpp

#include "Fighter.h"
Fighter::Fighter(int engin, int seat, string description)
{
    fNumEngines = engi;
    fNumSeats = seat;
    rangeAndSpeedDesc = description;
}

Fighter.h

#include "Airplane.h"

using namespace std;

#ifndef FIGHTER_H_
#define FIGHTER_H_

class Fighter:public Airplane {
private:
    int fNumSeats;
    int fNumEngines;
    string rangeAndSpeedDesc;
}

Airplane.cpp

#include "Airplane.h"

using namespace std;
Airplane::Airplane(int engines, int seats)
    {
        numSeats = seats;
        numEngines = engines;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...