有继承基类的麻烦

时间:2012-04-09 23:10:45

标签: c++ inheritance

当我继承基类时,它告诉我没有这样的类

这是增强的.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;
    };

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

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

我没有包含驱动程序文件或changeDispenser imp文件,但在驱动程序中包含了这些文件

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

2 个答案:

答案 0 :(得分:1)

首先,您需要将类changeDispenser的标头放在单独的头文件中,并将其包含在派生类头中。

changeDispenser没有默认的非参数构造函数,因此您需要在派生类中显式初始化它。一些事情:

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

或者您可以为构造函数参数定义默认值,由于样式原因这不太适合。

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

答案 1 :(得分:1)

如果您正确发布的源代码显示组成这组类的三个文件(enhanced.h,enhanced.cpp(?),changeDispencer.h),那么您应该添加

#include "changeDispenser.h"

到“enhanced.h”的顶部,当代码的某些部分包含changeDispenser的定义(来自“enhanced.h”时,始终确保enhanced的定义可用。 )。要对类进行子类化,基类的完整定义必须始终可用。