C++派生类继承方法错误

C++ Derived Class Inheritance Method Error

本文关键字:方法 错误 继承 派生 C++      更新时间:2023-10-16

我已经有一段时间没有用C++编程了,我正在尝试做一些练习题来再次熟悉语法。 我正在编写一个程序,该程序具有具有 3 个派生类的基类RetailEmployeeSalesEmployeeWarehouseEmployeeManagerEmployee 。 我在其中一个派生类的标头顶部有以下代码:

// Sales Employee Class Header
#indef SalesEmployee
#define SalesEmployee
#include <stdio.h>
#include "RetailEmployee.h"
using namespace std;
class SalesEmployee
{
public:
    SalesEmployee(string department, float pay, int ID, string name)
.
.
.

但是,每当我尝试在 SalesEmployee 实例上使用基类中的方法时,都会收到一条错误消息,指出找不到该方法。 此外,所有文件都位于同一目录中。

有人有什么建议吗?

您尚未指示编译器class SalesEmployeeclass RetailEmployee 的后代。为此,您应该:

class SalesEmployee : public RetailEmployee
{
}

您还需要更改构造函数,以便class SalesEmployee将必要的构造初始化信息传递给class RetailEmployee。例如在您的 SalesEmployee .cpp 实现文件中的以下内容:

SalesEmployee::SalesEmployee(string department, float pay, int ID, string name) : RetailEmployee( department, pay, ID, name ) 
{
    // Whatever special initialization SalesEmployee has goes here.
}

我假设所有这些数据成员实际上是在基类中定义的,因为它们应该对所有类都是通用的。