2012-04-10 58 views
1

我花了好几个小时研究并试图弄清楚为什么我得到这个错误。基本上,与继承有关的三个文件是CollegeMember.h,Employee.h和EmpAcademicRecord.h。雇员。从CollegeMember.h继承,EmpAcademicRecord.h从Employee.h继承。 Like this CollegeMember < - 员工< - EmpAcademicRecord。该错误发生在EmpAcademicRecord.h中。这里有三个文件。'{'标记之前的预期类名。 C++继承

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(); 

     }; 

任何帮助,这将不胜感激。

回答

3

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

在这种情况下,看来,你可能已经包括EmpAcademicRecord.h没有最早列入Employee.h(在包括在前者的顶部没有显示后者)。

换句话说,在这里编译器看到了这一点:

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

它有没有想法Employee类是什么。

可能是添加一个简单的问题:

#include "Employee.h" 

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

既然你有EmpAcademicRecord.h包含在Employee.h之内,那可能会导致无限递归。

你可以修复包括守卫,但我不能看到为什么你需要那particuli包含。 EmpAcademicRecord取决于Employee,而不是反过来。

+0

如果您包含“employee.h”,您将不得不放置在标头警卫中,因为“employee.h”包含该文件。 – chris 2012-04-10 02:53:42

+0

感谢您的快速反馈!我实际上尝试过,devC++似乎冻结了。在一个不同的尝试中,我把#include“Employee.h”放在EmpAcademicRecord类中,然后把#include“EmpAcademicRecord.h”放在主要的.cpp文件中(我应该包含这个文件),并且它摆脱了错误。再次感谢! – user1323056 2012-04-10 02:56:19

+0

因此,“可能是一件简单的事情”的评论,但我会添加到答案。 – paxdiablo 2012-04-10 02:57:10