继承错误:'{'标记之前的预期类名
Inheritance error: expected class-name before '{' token
我已经尝试了我能想到的#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
中删除评论即可
相关文章:
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 在 c++ 中拆分类和标头中的继承,错误
- enable_if继承的成员函数的名称查找错误
- C++模板类 - 继承调用错误的函数
- C++ 中的构造函数、继承、堆栈、堆、this-pointer 和段错误
- 为什么这个混合继承程序给出错误的输出?
- 使用多重继承时出现编译错误
- 多级继承错误 C2280:"atttempting to reference a deleted function"
- 相对于继承的构造函数,gcc 编译器是否还有一个错误?
- 从模板继承的 MSVC DLL 导出类会导致LNK2005已定义的错误
- 模板化类继承的子类定义中的错误
- C++错误LNK2005和不同项目文件的常规继承
- 错误:'int_type'未命名类型 - 如何继承 typedefs 和用法
- 在 soap_ssl_server_context(..) 中使用继承时 gsoap 中的分段错误
- C++中的继承,"...is an ambiguous base of ..."错误
- 与lambda一起使用虚拟继承在初始化列表中捕获此问题的GCC错误
- 收到未定义的引用错误 c++ 继承/多态性
- c++错误:继承转换无效
- 错误继承不能访问c++的方法