no appropriate default constructor available error

こ雲淡風輕ζ 提交于 2020-01-10 05:30:10

问题


here is my code:

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;
}

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.


回答1:


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.

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) {
    }
    //..
};



回答2:


twoDayPackage::twoDayPackage(double FlatFee):
  flatFee(FlatFee)

is calling the base constructor package(), because you haven't specified anything else.

Add a line package::package(){}; in class package :)




回答3:


You need a constructor for package. Also you don't need the package::package(...) when declaring the constructor ( that's for when you define it in the cpp file. ) Just package(...) will be just fine.

class package
{
protected:
    string name;
    string city;
    string state;
    int zip;
    double weight;
    double costPerOunce;

public:
    package()
    {}
    // \/ You don't need package:: that's only needed when you define the func in cpp
    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);
    }

};


来源:https://stackoverflow.com/questions/16434234/no-appropriate-default-constructor-available-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!