gpt4 book ai didi

c++ - 继承错误 : expected class-name before '{' token

转载 作者:搜寻专家 更新时间:2023-10-31 01:12:46 25 4
gpt4 key购买 nike

我已经尝试了所有我能想到的 #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 header 类的顶部:

#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;
}

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

最佳答案

你必须

#include "Employee.h"

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

中删除注释

关于c++ - 继承错误 : expected class-name before '{' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13391706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com