没有适当的默认构造函数可用错误 [英] no appropriate default constructor available error
问题描述
这里是我的代码:
class package
{
protected:
string name;
string city;
string state;
int zip;
double weight;
double costPerOunce;
public:
package::package(string Name, string City, string State, int Zip, double Weight, double CostPerOunce):
name(Name), city(City), state(State),
zip(Zip), weight(Weight), costPerOunce(CostPerOunce)
{
}
double calculateCost()
{
return (weight * costPerOunce);
}
};
class twoDayPackage: public package
{
protected:
double flatFee;
public:
twoDayPackage::twoDayPackage(double FlatFee):
flatFee(FlatFee)
{
}
double calculateCost()
{
return (weight * costPerOunce) + flatFee;
}
};
int main()
{
system ("pause");
return 0;
}
我试图运行这个代码,我得到的错误如下:
错误C2512:'package':没有适当的默认构造函数
i try to run this code and the error i get is as follows: error C2512: 'package' : no appropriate default constructor available
错误与基类构造函数的继承有关,但我不知道为什么代码不运行。请帮助我。
the error has something to do with inheritance of the base class constructor but i don't know exactly why the code isn't running. please help me.
推荐答案
twoDayPackage
的构造函数将首先创建 package
,然后构造 flatFee
。因为你不告诉它如何做,它寻找一个默认的方式来构造 package
。
The constructor for twoDayPackage
will first create package
, before constructing flatFee
. As you don't tell it how to do that, it looks for a default way of constructing package
.
当你构造 twoDayPackage
时,你需要给它构建基础的包
所需的一切。或者让它确定要传递给包
构造函数的值。
When you construct twoDayPackage
you need to give it everything it needs to construct the underlying package
. Either that, or have it determine values to pass to the package
constructor.
传递所需的参数看起来像这个:
Passing in the required parameters looks like this:
class twoDayPackage {
public:
twoDayPackage(string Name, string City, string State, int Zip, double Weight, double CostPerOunce, double flatFee) :
package(Name, City, State, Zip, Weight, CostPerOunce),
flatFee(flatFee) {
}
//..
};
这篇关于没有适当的默认构造函数可用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!