2

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.

2
  • 4
    please learn the basic syntax... May 8, 2013 at 6:40
  • When you write your own constructor, the default one will be overridden. twoDayPackage::twoDayPackage(double FlatFee): will call it, but it doesn't exists.
    – Maroun
    May 8, 2013 at 6:43

3 Answers 3

4
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 :)

7
  • 1
    +1, but a default constructor means he'd not be initializing any of those other members (weight, etc...). But yeah - that's probably beyond the scope of this question :)
    – Moo-Juice
    May 8, 2013 at 6:43
  • Moo-Juice - yeah, a short answer not a complete answer. I find solving one problem at a time helps when trying to learn c++ syntax.
    – Melanie
    May 8, 2013 at 6:47
  • 1
    Well, the three string ones would be.
    – chris
    May 8, 2013 at 6:49
  • This doesn't tell OP the dangers though.
    – Peter Wood
    May 8, 2013 at 7:14
  • i have added the line and the program runs. now i want to make the class twoDayPackage take values of the class package. please help.
    – Mwiti
    May 8, 2013 at 14:12
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);
    }

};
2

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
  • what is the syntax for what you are suggesting?
    – Mwiti
    May 8, 2013 at 13:46
  • 1
    you my friend, are a true ninja. thank you very much. My code is now running exactly how i wanted it to. Again, Muchos Gracias!!!
    – Mwiti
    May 8, 2013 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.