[英]C++ Derived Class Inheritance Method Error
自從我用C ++編程以來已經有一段時間了,我試圖做一些練習問題以再次熟悉語法。 我正在寫有一個基類節目RetailEmployee
3派生類: SalesEmployee
, WarehouseEmployee
和ManagerEmployee
。 我的派生類之一的標頭頂部有以下代碼:
// 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 SalesEmployee
是class 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.
}
我假設所有這些數據成員實際上都是在基類中定義的,因為它們對於所有類都是通用的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.