-1

i'm trying to fix this error but i don't what to do..

#include<iostream>
#include<string>
using namespace std;
class AirShip{
private:
    int passenger;
    double cargo;
public:
    AirShip(int x,double y)
    {
        passenger=x;
        cargo=y;
    }

    void show ()
    {
        cout<<"passenger="<<passenger<<endl;
        cout<<"cargo="<<passenger<<endl;
    }
};
class AirPlane: protected AirShip{
private:
    string engine;
    double range;
public:
    AirPlane(string a,double b)
    {
        engine=a;
        range=b;
    }
void show()
{
    cout<<"engine="<<engine<<endl;
    cout<<"range="<<range<<endl;
}
};

the error is : error: no matching function for call to 'AirShip::AirShip()' need help on this... i'll put the main function later ,since the error is here.

2 Answers2

3

When you create an AirPlane you implicitly also create the AirShip part of it. You could have written the constructor also like this:

AirPlane(string a,double b) : AirShip()
{
    engine=a;
    range=b;
}

However, AirShip has no default constructor. You basically have two choices:

A) provide a default constructor. A default constructor is one that can be called without arguments. You could for example provide default arguments for the number of passengers and cargo. However, I would not recommend that. Imho it is best to correctly initialize all members in the constructor and default values are not what you want most of the time.

B) correctly initialize the AirShip part of your AirPlane for example...

AirPlane(string a,double b, int x, double y) : 
   AirShip(x,y),engine(a),range(b)
{}

...and use the initializer list also for the other members.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • i see yeah it initialized but how should i call main function...i'm trying to understand c++ ..i wrote the main function like this---- int main(void){ AirPlane ap1(200,40.550,"jets",500.560); AirPlane ap; ap.show(); return 0; } again i get the same error....so,do i have to call constructor AirShip @ the main funtion too – johner jubair Apr 13 '17 at 21:31
  • @johnerjubair you still have a `AirPlane ap;` in your `main`, ie this time you are trying to call the default constructor of `Airplane`, this time your options are A) provide default constructor for `Airplane` (would not suggest) B) just pass the arguments as you did it in `ap1(200,40.550,"jets",500.560)`, e.g. `ap(100,1.0,"waterbaloon",0.0)` – 463035818_is_not_an_ai Apr 13 '17 at 21:40
  • thank you.It worked nicely .That taught me something :) – johner jubair Apr 13 '17 at 21:47
  • @johnerjubair if you are satisfied with one of the answers you should accept it (not necessarily mine ;) – 463035818_is_not_an_ai Apr 13 '17 at 22:09
0

Since your AirPlane-class derives from AirShip, it needs to initialise the base class as well, either through an explicitly called AirShip-constructor or through an (even implicitly) called AirShip-default constructor.

However, your AirShip class doesn't provide a default constructor (which could be implicitly called), and your AirPlane(string a,double b) { ... }-constructor does not explicitly call any other AirShip-constructor.

So either define a default constructor at AirShip, or introduce an explicit initialisation of the base class, e.g. by writing AirPlane(string a,double b) : AirShip(0,0) {...}.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58