'{'标记前的预期类名. C ++继承 [英] expected class name before '{' token. C++ inheritance

查看:78
本文介绍了'{'标记前的预期类名. C ++继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间研究并试图弄清楚为什么我会收到此错误.基本上,与继承有关的三个文件是CollegeMember.h,Employee.h和EmpAcademicRecord.h.员工.是从CollegeMember.h继承而EmpAcademicRecord.h是从Employee.h继承.像这样的CollegeMember<-雇员<-EmpAcademicRecord.该错误发生在EmpAcademicRecord.h中.这是三个文件.

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

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

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

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.

推荐答案

这种错误通常是由于尝试使用该类型时未定义类型引起的.

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

在这种情况下,您似乎包含了EmpAcademicRecord.h 而没有首先包含了Employee.h(前者顶部的包含并不显示后者).

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

不知道Employee类是什么.

添加 可能很简单:

#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.

由于Employee.h中包含了EmpAcademicRecord.h,因此可能会导致无限递归.

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

您可以使用包含卫士来解决此问题,但我看不到为什么需要使用特定的包含. EmpAcademicRecord取决于Employee,而不取决于 .

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.

这篇关于'{'标记前的预期类名. C ++继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆