2017-03-16 109 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