0

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

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

这是增强的.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"
4

2 回答 2

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);
于 2012-04-09T23:15:06.723 回答
1

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

#include "changeDispenser.h"

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

于 2012-04-09T23:15:26.350 回答