2017-03-16 104 views
0

我對編程相當陌生,而且我遇到了無法解決的問題。我嘗試了所有我能想到的。我準備好這是一個簡單的錯誤。C++派生類不能正常工作

的main.cpp

#include <iostream> 
#include <iomanip> 
#include "new_employee.h" 
#include "new_employee.cpp" 
#include "permanent_employee.cpp" 

using namespace std; 

int in_employee[4] = {101, 102, 103, 104}; 
int in_bankaccount[4] = {80045001, 80045002, 80045003, 80045004}; 
float in_hours[4] = {40, 50, 50, 51}; 
float in_rate[4] = {22, 22, 24, 26}; 

int main() 
{ 
    for(int i=0;i<4;i++) 
{ 
    new_employee employee[i](in_employee[i], in_bankaccount[i]); 
} 
///permanent_employee employee2(in_employee[1], in_bankaccount[1]); 
///permanent_employee employee3(in_employee[2], in_bankaccount[2]); 
///permanent_employee employee4(in_employee[3], in_bankaccount[3]); 
} 

new_employee.h

#if !defined NEW_EMPLOYEE 
#define NEW_EMPLOYEE 

class new_employee 
{ 
public: 
    new_employee(); 
    new_employee(int employee_number, int account_number); 
private: 
    int employee_no, account_no; 
    float hourly_rate, hours_worked; 
}; 

class permanent_employee : public new_employee 
{ 
public: 
    permanent_employee(); 
    permanent_employee(int employee_number, int account_number); 
private: 
    float union_deduction, vhi_deduction; 
}; 
#endif 

new_employee.cpp

#include <iostream> 
#include <iomanip> 
#include "new_employee.h" 

using namespace std; 

new_employee::new_employee() 
{ 
    employee_no = 0; 
    account_no = 0; 
} 

new_employee::new_employee(int employee_number, int account_number) 
{ 
    employee_no = employee_number; 
    account_no = account_number; 
} 

permanent_employee.cpp

#include <iostream> 
#include <iomanip> 
#include "new_employee.h" 

using namespace std; 

permanent_employee::permanent_employee() 
{ 
    employee_no = 0; 
    account_no = 0; 
} 

permanent_employee::permanent_employee(int employee_number, int account_number) 
{ 
    employee_no = employee_number; 
    account_no = account_number; 
} 

因此,我甚至沒有嘗試正確運行該程序的原始功能,因爲直接從Codeblocks複製了以下錯誤。

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h||In constructor 'permanent_employee::permanent_employee()':|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::employee_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|9|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::account_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|10|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h||In constructor 'permanent_employee::permanent_employee(int, int)':|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::employee_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|15|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::account_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|16|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\main.cpp||In function 'int main()':|

Z:\C++\Assignment 3 - Payroll Processing\main.cpp|18|error: variable-sized object 'employee' may not be initialized|

Z:\C++\Assignment 3 - Payroll Processing\main.cpp|18|warning: unused variable 'employee' [-Wunused-variable]| ||=== Build finished: 9 errors, 1 warnings (0 minutes, 0 seconds) ===|

我想用派生類permanent_employee做一個基類new_employee。對我來說,看起來每個人都試圖訪問其他的變量。我會很感激任何反饋。

謝謝你的幫助。

PS。我是新來的這個網站,所以我很抱歉,如果我發佈不正確。

+5

您可能要遵循[不錯的C++的書(http://stackoverflow.com/q/388242/1782465)獲得C++的語義和概念,如繼承紮實抓好。它會給你一個比堆棧溢出分段問題更好的基礎。 – Angew

+5

永遠不要包含*。在其他文件中的cpp文件 – CinCout

+0

你如何理解錯誤消息「在構造函數permanent_employee :: permanent_employee()':'int new_employee :: employee_no'是私有的? – Angew

回答

1

您有您的成員變量的可訪問性問題。一般可訪問是這樣的:

公衆:任何人,每個人都可以看到和更改這些傢伙。 受保護:包含這些變量作爲成員和任何派生類的類可以更改這些變量。外部課程無法訪問它們。 Private:只有包含這些成員變量的類可以以任何方式修改或使用它們。

該錯誤是由permanent_employee類試圖訪問new_employee類的私有成員造成的。您也可以嘗試從派生構造函數調用基類構造函數。

你走我非常

無論哪種方式,強烈建議您花一些時間,你做任何事情之前要充分了解公衆,保護和私有成員變量和函數之間的差異。這將使你的生活從長遠來看變得更容易。

+0

我可能會誤解你來自哪裏(以及一般的繼承)。我試圖用X變量和帶有X + Y變量的派生類'permanent_employee'來創建一個'new_employee'類。我試圖改變「permanent_employee」的,它應該從‘new_employee’ – Jordan

+1

繼承@Jordan它沒有繼承它只是不能訪問它們由於權限的變量X變量的副本。私人意味着只有聲明的類可以訪問它們; 'new_employee'。繼承的類不能。受保護意味着只有聲明和繼承類可以訪問它們; 'new_employee'和/或'permanent_employee'。嘗試從私人切換到受保護的狀態,您會看到。但是,正如我所說,這是一個非常關鍵的理解概念,在繼續學習之前,您應該花時間閱讀並進行實驗。 – soulsabr

2

變化:

permanent_employee::permanent_employee(int employee_number, int account_number) 
{ 
    employee_no = employee_number; 
    account_no = account_number; 
} 

調用基類的構造函數:

permanent_employee::permanent_employee(int employee_number, int account_number) 
: new_employee(employee_number, account_number) 
{ 
} 

由於成員變量聲明爲privatenew_employee,他們不能訪問甚至派生類。如果你希望派生類能夠修改它們(但有時你不會因爲不變的保存),你可以聲明它們爲protected