6

我花了好几个小时研究并试图弄清楚为什么我会收到这个错误。基本上与继承有关的三个文件是 CollegeMember.h、Employee.h 和 EmpAcademicRecord.h。员工。是从 CollegeMember.h 继承的,而 EmpAcademicRecord.h 是从 Employee.h 继承的。喜欢这个 CollegeMember <- Employee <- EmpAcademicRecord。EmpAcademicRecord.h 中出现错误。这是三个文件。

学院会员.h

#include <cstdlib>
#include <iostream>
#include<ctype.h>
#include<string.h>
#include "Employee.h"
#include "Student.h"
using namespace std;

// ****************************************************************************  
// Class Definitions follow
typedef char* String;

// The CollegeMember class
class CollegeMember
{
  protected:
 int ID_Number;
 string FirstName, LastName;
 string AddressLine1, AddressLine2, StateProv, Zip;
 string Telephone;
 string E_Mail;
 string answer, answer2, answer3, answer4;//used as sort of booleans for use with        validation

 // member functions
public:
 CollegeMember ( ); // constructor
 CollegeMember(const CollegeMember&); //overloaded constructor
 void Modify (CollegeMember Member);
 void InputData(int x);
 string Summary ( ); //summary
 string PrintMe(); //fully describes
}; // End of CollegeMember class declaration

员工.h

#include <cstdlib>
#include <iostream>
#include<ctype.h>
#include<string.h>
#include "EmpAcademicRecord.h"
#include "EmpEmploymentHistory.h"
#include "EmpExtraCurricular.h"
#include "EmpPersonalInfo.h"
#include "EmpPublicationLog.h"

using namespace std;

// ****************************************************************************
// Class Definitions follow
typedef char* String;

// The Employee Class
class Employee: protected CollegeMember
{
 float Salary;
 protected:
 string Department, JobTitle;
 // Member Functions
public: 
 Employee ( );      // constructor
void Modify (Employee ThisEmp);
 void InputData(int x);
 void SetSalary (float Sal) // Specified as an in-line function
 { Salary = Sal;}
 float GetSalary ( ) {return Salary;} // Specified as an in-line function
string Summary ( ); //summary 
 string PrintMe(); //fully describes
}; // End of Employee class declaration

EmpAcademicRecord.h

#include <iostream>
#include <cstdlib>
#include<ctype.h>
#include<string.h>

using namespace std;

typedef char* String;

class EmpAcademicRecord: protected Employee{ //error occurs on this line
      protected:
                int ReferenceNumber;
                string Institution;
                string Award;
                string start;
                string end;

                public:
                       EmpAcademicRecord();
                       void InputData (int x);
                       void Modify(EmpAcademicRecord ThisRec);
                       void Summary();

      };

对此的任何帮助将不胜感激。

4

1 回答 1

8

这种错误通常是由于您尝试使用它时未定义类型引起的。

在这种情况下,您可能在EmpAcademicRecord.h 没有首先包含的情况下包含Employee.h了(前者顶部的包含不显示后者)。

换句话说,在编译器看到的地方:

class EmpAcademicRecord: protected Employee { //error occurs on this line

它不知道Employee类是什么。

添加以下内容可能很简单:

#include "Employee.h"

到该文件的顶部,由于我们没有代码文件,因此有点难以确定。无论如何,这肯定是一个很好的第一步。

由于您已EmpAcademicRecord.h被 包含Employee.h,这可能会导致无限递归。

你可以用包含守卫来解决这个问题,但我不明白为什么你需要那个特殊的包含。EmpAcademicRecord取决于Employee而不是相反。

于 2012-04-10T02:45:17.980 回答