我花了好几个小时研究并试图弄清楚为什么我会收到这个错误。基本上与继承有关的三个文件是 CollegeMember.h、Employee.h 和 EmpAcademicRecord.h。员工。继承自 CollegeMember.h 和 EmpAcademicRecord.h 继承自 Employee.h。像这样 CollegeMember
CollegeMember.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();
};
对此的任何帮助将不胜感激。
最佳答案
这种错误通常是由于您尝试使用它时未定义类型造成的。
在这种情况下,您可能在没有首先包含 EmpAcademicRecord.h
的情况下包含了 Employee.h
(前者顶部的包含不显示后者)。
换句话说,在编译器看到的地方:
class EmpAcademicRecord: protected Employee { //error occurs on this line
它不知道
Employee
类是什么。添加以下内容可能很简单:
#include "Employee.h"
到该文件的顶部,由于我们没有代码文件,因此很难确定。无论如何,这肯定是好的第一步。
由于
EmpAcademicRecord.h
包含 Employee.h
,因此可能会导致无限递归。你可以用包含守卫来解决这个问题,但我不明白为什么你需要那个特殊的包含。
EmpAcademicRecord
取决于 Employee
,而不是相反。