expected class name before '{' token. C++ inheritance

夙愿已清 提交于 2020-12-05 12:00:24

问题


I've spent quite a few hours researching and trying to figure out why I'm getting this error. Basically the three files that have to do with the inheriting are CollegeMember.h, Employee.h, and EmpAcademicRecord.h. Employee. is inheriting from CollegeMember.h and EmpAcademicRecord.h is inheriting from Employee.h. Like this CollegeMember <- Employee <- EmpAcademicRecord. The error occurs in EmpAcademicRecord.h. Heres the three files.

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

      };

Any help with this would be greatly appreciated.


回答1:


That sort of error is usually caused by the type not being defined when you try to use it.

In this case, it appears that you may have included EmpAcademicRecord.h without having first included Employee.h (the includes at the top of the former do not show the latter).

In other words, at the point where the compiler sees:

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

it has no idea what the Employee class is.

It may be a simple matter of adding:

#include "Employee.h"

to the top of that file, it's a little difficult to be certain since we don't have the code files. In any case, it's certainly a good first step.

Since you have EmpAcademicRecord.h being included by Employee.h, that will probably result in an infinite recursion.

You could fix that with include guards, but I can't see why you need that particulat inclusion. EmpAcademicRecord depends on Employee, not the other way around.



来源:https://stackoverflow.com/questions/10082488/expected-class-name-before-token-c-inheritance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!