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