【问题标题】:Inheritance error: expected class-name before '{' token继承错误:“{”标记之前的预期类名
【发布时间】:2012-11-15 04:48:24
【问题描述】:

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

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

【问题讨论】:

  • 您应该用您使用的语言标记问题。届时会有更多人看到您的问题。
  • 对于哪个文件和哪一行出现错误?
  • Executive.h line 12 class Executive : public Employee {
  • 为什么要在主程序中包含.cpp 文件?这不是通常的做法。
  • 你如何构建这个?您是否分别编译每个 .cpp 文件(使用 GCC,您将使用 -o Executive.o -c Executive.cpp 执行此操作),最后将它们链接在一起?在这种情况下,您是在编译Executive.cpp 时,还是在编译其他文件之一时出现错误?

标签: c++ class inheritance


【解决方案1】:

你必须

#include "Employee.h"

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

【讨论】:

  • 谢谢 - 消除了那个错误,但随后出现了一堆重新定义错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 2021-09-22
  • 2015-01-19
相关资源
最近更新 更多