2012-02-19 155 views
1

我有一些关于继承的问题。我有一类Person,和一类Student:Person,Employee:Person。我越来越困惑的错误 - 我不明白为什么我要得到它们。我使用微小的粘贴来粘贴代码,因为我认为这会占用太多空间。如果我应该在其他地方发表问题,请告诉我。谢谢。C++继承问题

代码文件:

下面是我收到的错误:

1>------ Build started: Project: PR4_Students, Configuration: Debug Win32 ------ 
1>Build started 2/18/2012 11:14:27 PM. 
1>InitializeBuildStatus: 
1> Touching "Debug\PR4_Students.unsuccessfulbuild". 
1>ClCompile: 
1> main.cpp 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}' 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2059: syntax error : 'inline function header' 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2630: ';' found in what should be a comma-separated list 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}' 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2059: syntax error : 'inline function header' 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2630: ';' found in what should be a comma-separated list 
1> Student.cpp 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}' 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2059: syntax error : 'inline function header' 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2630: ';' found in what should be a comma-separated list 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.cpp(8): error C2084: function 'Student::Student(void)' already has a body 
1>   \\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(15) : see previous definition of '{ctor}' 
1> Employee.cpp 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}' 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2059: syntax error : 'inline function header' 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2630: ';' found in what should be a comma-separated list 
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.cpp(8): error C2084: function 'Employee::Employee(void)' already has a body 
1>   \\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(15) : see previous definition of '{ctor}' 
1> Generating Code... 
1> 
1>Build FAILED. 
1> 
1>Time Elapsed 00:00:05.64 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+1

欢迎来到SO的C++社区,Michael!在将来,通过标记与家庭作业有关的问题来获得布朗尼分数[标签:家庭作业]。 – Potatoswatter 2012-02-19 07:38:20

+0

'学生():人();'是错的。您只需要在构造函数的定义中提供初始化程序列表,而不是在声明中。试试'Student();'。 – 2012-02-19 07:39:40

回答

4

我没有分析整个代码,但你似乎很困惑如何声明调用基类的构造函数;

class Student : public Person 
{ 
... 
    Student() : Person(); 
... 
}; 

基类的构造函数的调用应在实际执行仅构造来完成。既然你已经做到这一点与

Student::Student() : Person() { 

你可以改变的声明

class Student : public Person 
{ 
... 
    Student(); 
... 
}; 

和事情的结果会更好。

编辑:在下面的后续问题中添加答案;

线

Employee(string department, string jobTitle, int yearOfHire) 
    : Person(name, socialSecurityNumber, age, gender, address, phoneNumber) { 

确实出于同样的原因没有真正意义。如果您希望能够使用所有这些参数构建一个Employee,则需要将该构造函数声明为;

Employee(string department, string jobTitle, int yearOfHire, name, 
     socialSecurityNumber, age, gender, address, phoneNumber) { 

并执行它作为

Employee::Employee(string department, string jobTitle, int yearOfHire, name, 
     socialSecurityNumber, age, gender, address, phoneNumber) 
    : Person(name, socialSecurityNumber, age, gender, address, phoneNumber) { 

从而传递参数到基类的构造。

+0

谢谢。我如何使用Person和Student参数创建一个Student对象? – michaellindahl 2012-02-19 07:52:27

+0

可能在这里完全误解了你的问题,但是为了让它使用传递给Student构造函数的参数调用Person构造函数,只需将实现行更改为'Student :: Student(int age):Person(age){'。当然,Person需要一个构造函数来实际调用以年龄作为参数。 – 2012-02-19 07:59:00

+0

让我给你举个例子。目前我认为我的IDE需要这样的代码:'personVector.push_back(new Employee(“Sales”,“VP”,2008));'然而我认为它是这样的:'personVector.push_back(new Employee(“Sales “,”VP“,2008,”John“,123456789,19,'m',”Unknown“,3218424222));' – michaellindahl 2012-02-19 08:04:58

4

在Students.h的第15行:

Student() : Person(); 

这是无效的。要么你需要在那里完全定义构造函数,要么根本不需要。

所以:

Student() : Person() { some code; }; 

或:

Student(); 

,并在您实现文件的实际代码。

+0

您可以在实现文件中使用默认构造函数和覆盖构造函数吗?我认为覆盖的构造函数需要在头文件中。 – michaellindahl 2012-02-19 07:51:35

+0

我不明白你的问题 - 什么是覆盖的构造函数?除非你处理模板,否则你可以自由地将东西放在标题或实现文件中。你不能做的是像你一样的部分构造函数定义。 – Mat 2012-02-19 07:54:24

+0

好的。我懂了。我在谈论:'Student()','Student(int age)'和'Student(string name,int age)'。我被告知除了'Student()'以外的任何东西都需要在.h文件中。但根据你说的他们实际上应该在.cpp文件中? – michaellindahl 2012-02-19 08:07:29