gpt4 book ai didi

c++ - 继承基类有问题

转载 作者:行者123 更新时间:2023-11-28 03:38:33 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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);

关于c++ - 继承基类有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10081027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com