2

我已经尝试了所有#include我能想到的语句组合,但没有任何效果。我正在尝试编写一个基本的继承程序,但我不断收到错误error: expected class-name before '}' token,我只是不知道该怎么办了。我试过让我main()包含.cppExecutive类的文件,但是出现了这个错误。该程序包括 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;
}

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

4

1 回答 1

4

你必须

#include "Employee.h"

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

于 2012-11-15T06:03:15.803 回答