0

So I have a class named package with has a bunch of variables. I have all of the get/set methods AND a constructor working.

package header code

threeDay header code

twoDay header code

package class code

threeDay class code

twoDay class code

I have two derived class named twoDay and threeDay that inherit the package class and need to use its constructor.

Constructor for the package class:

package::package(string sN, string sA, string sC, string sS, int sZ, string rN, string rA, string rC, string rS, int rZ, int w, int c) {

    this->senderName = sN;
    this->senderAddress = sA;
    this->senderCity = sC;
    this->senderState = sS;
    this->senderZip = sZ;

    this->receiverName = rN;
    this->receiverAddress = rA;
    this->receiverCity = rC;
    this->receiverState = rS;
    this->receiverZip = rZ;

    this->weight = w;
    this->cpo = c;


}

I've been using this code for the constructor in the threeDay header:

threeDay(string, string, string, string, int, string, string, string, string, int,int,int);

What I need to have happen is to have twoDay and threeDay to be able to use the constructor. What I mean is that the derived packages need to be able to use the base classes constructor.

I currently get this error:

threeDay.cpp:10:136: error: no matching function for call to ‘package::package()’

I did some research from this link: http://www.cs.bu.edu/teaching/cpp/inheritance/intro/

and this link: C++ Constructor/Destructor inheritance

So it seems like I don't directly inherit the constructor, I still need to define it. If that is the case, than why isn't my code working now?

but I just can't seem to get it to work.

Once I get the constructors working, it's smooth sailing from there.

Community
  • 1
  • 1
Zeratas
  • 1,005
  • 3
  • 17
  • 36

1 Answers1

3

Since package doesn't have a default constructor (that is, one which takes no arguments), you need to tell your derived classes how to build a package.

The way to do this is to call the base class constructor in the derived class's initialiser list, like so:

struct Base
{
    Base(int a);
};

struct Derived : public Base
{
    Derived(int a, string b) : Base(a) { /* do something with b */ }
};
Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82