问题描述
我正在尝试修复此错误,但我不知道该怎么做..
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;
}
};
错误是:
错误:没有用于呼叫'AirShip :: AirShip的匹配功能()'
需要这方面的帮助...
我将稍后放置主函数,因为错误就在这里。
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.
推荐答案
当您创建 AirPlane
时,您还隐式地创建了 AirShip
部分。您可以像这样编写构造函数:
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;
}
然而, AirShip
没有默认构造函数。您基本上有两种选择:
However, AirShip
has no default constructor. You basically have two choices:
A)提供默认构造函数。默认构造函数是可以不带参数调用的构造函数。例如,您可以为乘客和货物的数量提供默认参数。但是,我不建议这样做。 Imho最好正确地初始化构造函数中的所有成员,默认值不是你想要的大部分时间。
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)正确初始化 AirShip
部分 AirPlane
例如......
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.
这篇关于获取错误没有与AirShip AirShip呼叫匹配的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!