C ++继承问题

时间:2012-02-19 07:31:21

标签: c++ inheritance

我有一些关于继承的问题。我有一类人,一类学生:人,雇员:人。我遇到的错误令我感到困惑 - 我不明白为什么我会得到它们。我使用微小的粘贴来粘贴代码,因为我认为它会占用太多空间。如果我应该在别处发布问题,请告诉我。感谢。

代码文件:

以下是我遇到的错误:

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 ==========

2 个答案:

答案 0 :(得分:4)

在Students.h的第15行:

Student() : Person();

那是无效的。您需要在那里完全定义构造函数,或者根本不需要。

所以:

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

或:

Student();

并将实际代码放在您的实现文件中。

答案 1 :(得分: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) {

因此将参数传递给基类构造函数。