我花了好几个小时研究并试图弄清楚为什么会出现此错误。基本上与继承有关的三个文件是 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();
};
如有任何帮助,我们将不胜感激。
最佳答案
这种错误通常是由于您尝试使用它时未定义类型引起的。
在这种情况下,看起来您可能已经包含了 EmpAcademicRecord.h
而没有首先包含 Employee.h
(包含在前者的顶部不显示后者)。
换句话说,在编译器看到的地方:
class EmpAcademicRecord: protected Employee { //error occurs on this line
它不知道 Employee
类是什么。
这可能是一个简单的添加问题:
#include "Employee.h"
到该文件的顶部,由于我们没有代码文件,因此很难确定。无论如何,这无疑是良好的开端。
由于 Employee.h
包含了 EmpAcademicRecord.h
,这可能会导致无限递归。
你可以用 include guards 解决这个问题,但我不明白为什么你需要那个特殊的包含。 EmpAcademicRecord
取决于 Employee
,不相反。
关于c++ - '{' 标记之前的预期类名。 C++继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10082488/