继承错误:'{'标记之前的预期类名

Inheritance error: expected class-name before '{' token

本文关键字:错误 继承      更新时间:2023-10-16

我已经尝试了我能想到的#include语句的所有组合,没有任何效果。我正在尝试编写一个基本的继承程序,但我不断收到错误error: expected class-name before '}' token,我只是不知道该怎么办了。我尝试让我的main()包含执行类的.cpp文件,但是出现了此错误。该程序包括 5 种类型的员工,它们都继承自 Employee 类,我假设它们都是相同的错误:

#include <iostream>
#include <string>
#include "Employee.cpp"
#include "Manager.cpp"
#include "Executive.cpp"
#include "Technical.cpp"
#include "Software.cpp"
#include "Test.cpp"
using namespace std;
int main()
{
    Employee emp[3];
    Executive emp0("John", "Doe", "VP", 100000.0, 1000000.0, 2000.0);
    Software emp1("Vincent", "Giuliana", "Project Leader", 150000.0, 200000.0, 1000.0);
    Test emp2("Lauren", "Wallis", "Overseer of Testing", 95000, 115000);
    emp[0] = emp0;
    emp[1] = emp1;
    emp[2] = emp2;
    for(int i=0; i<3; i++)
        emp[i].displayInformation();
    emp0.displayInformation();
    emp1.displayInformation();
    emp2.displayInformation();
    return 0;
}

我的Employee.h头文件如下:

#ifndef EMPLOYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED
#include <string>
#include <iostream>
using namespace std;
class Employee
{
private:
    string fName, lName, jobTitle;
    double baseSalary, salary;
public:
    Employee();
    Employee(string fName, string lName, string jobTitle, double baseSalary);
    void calculateSalary(double baseSalary);
    void displayName();
    void displayBSalary();
    void displayJobTitle();
    void displayInformation();
...
getters
...
...
setters
...
};
#endif // EMPLOYEE_H_INCLUDED

Employee.cpp是:

#include <string>
#include <iostream>
#include "Employee.h"
using namespace std;
Employee::Employee()
{
    fName = "";
    lName = "";
    jobTitle = "";
    baseSalary = 000000;
}
...
void Employee::setBSalary(double bs) //sets base salary as parameter
{
    baseSalary = bs;
}

Executive.h标头类的顶部:

#ifndef EXECUTIVE_H_INCLUDED
#define EXECUTIVE_H_INCLUDED
#include <string>
#include <iostream>
//#include "Employee.h"
using namespace std;
class Executive : public Employee
{
private:
    string fName, lName, jobTitle;
    double baseSalary, salary, bonus, stockOption;
public:
...
};
#endif // Executive_H_INCLUDED

最后但并非最不重要的一点是,Executive.cpp文件... #include #include

#include "Executive.h"
using namespace std;
Executive::Executive()
{
    fName = fN;
    lName = lN;
    jobTitle = jt;
    baseSalary = bs;
    bonus = b;
    stockOption = so;
}
...
void Executive::setSO(double so) //sets stock option as parameter
{
    stockOption = so;
}

我认为我已经尝试在每个文件中包含每个标头,但仍然没有。任何帮助将不胜感激,我提前非常感谢任何人!

你必须

#include "Employee.h"

Executive.h中,因为当一个类继承自它时,编译器必须看到Employee的声明。因此,只需从#include中删除评论即可