Nick
Nick

Reputation: 702

Having troubles inheriting base class

When I inherit the base class, it's telling me there is no such class

This is enhanced.h:

  class enhanced: public changeDispenser // <--------where error is occuring
    {
    public:
        void changeStatus();
        // Function: Lets the user know how much of each coin is in the machine
        enhanced(int);
        // Constructor
        // Sets the Dollar amount to what the User wants
        void changeLoad(int);
        // Function: Loads what change the user requests into the Coin Machine
        int dispenseChange(int);
        // Function: Takes the users amount of cents requests and dispenses it to the user

    private:
        int dollar;
    };

This is enhanced.cpp:

#include "enhanced.h"
#include <iostream>
using namespace std;
enhanced::enhanced(int dol)
{
    dollar = dol;
}

void enhanced::changeStatus()
{
    cout << dollar << " dollars, ";
    changeDispenser::changeStatus();
}

void enhanced::changeLoad(int d)
{
    dollar = dollar + d;
    //changeDispenser::changeLoad;
}

This is changeDispenser.h:

class changeDispenser
{
public:
    void changeStatus();
    // Function: Lets the user know how much of each coin is in the machine
    changeDispenser(int, int, int, int);
    // Constructor
    // Sets the Quarters, Dimes, Nickels, and Pennies to what the User wants
    void changeLoad(int, int, int, int);
    // Function: Loads what change the user requests into the Coin Machine
    int dispenseChange(int);
    // Function: Takes the users amount of cents requests and dispenses it to the user
private:
    int quarter;
    int dime;
    int nickel;
    int penny;
};

I didn't include the driver file or the changeDispenser imp file, but in the driver, these are included

#include "changeDispenser.h"
#include "enhanced.h"

Upvotes: 0

Views: 71

Answers (2)

Dmitriy Kachko
Dmitriy Kachko

Reputation: 2914

Firstly, you need to put header of the class changeDispenser in separate header file and include it in derived class header.

The class changeDispenser doesn't have default non-argument constructor, so you need to initialize it explicitly in the derived class. Something along the lines:

enhanced::enhanced(int dol) : changeDispenser(0, 0, 0, 0)
{
    dollar = dol;
}

Or you could define default values for the constructor arguments, which is less preferable for stylistic reasons.

changeDispenser(int i=0, int j=0, int k=0, int l=0);

Upvotes: 1

modelnine
modelnine

Reputation: 1499

If the source code you posted correctly shows the three files (enhanced.h, enhanced.cpp (?), changeDispencer.h) making up this group of classes, then you should add

#include "changeDispenser.h"

to the top of "enhanced.h" to always make sure that the definition of changeDispenser is available when some part of your code includes the definition of enhanced (coming from "enhanced.h"). To subclass a class, the complete definition of the base class must always be available.

Upvotes: 1

Related Questions