C++ 什么';这有什么不对?包括Setters/getter(已编辑)头文件
当我在一个班里有下列成员时C++ 什么';这有什么不对?包括Setters/getter(已编辑)头文件,c++,setter,composition,getter,C++,Setter,Composition,Getter,当我在一个班里有下列成员时 employee headOfDepartment; 这些二传手和接球手怎么了 void department::setHeadOfDepartment( employee depEmployee) { headOfDepartment=depEmployee; } employee department::getHeadOfDepartment() { return headOfDepartment; } 我一
employee headOfDepartment;
这些二传手和接球手怎么了
void department::setHeadOfDepartment( employee depEmployee)
{
headOfDepartment=depEmployee;
}
employee department::getHeadOfDepartment()
{
return headOfDepartment;
}
我一直试图用组合定义setter和getter,但它总是让我犯这样的错误:“字段'headOfDepartment'的类型不完整”
好的,这些是头文件:
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
using namespace std;
#include <iostream>
#include <vector>
#include "department.h"
#include "project.h"
class department;
class project;
//#include <vector>
employee.h
class employee
{
string Name; //text with spaces
string National_ID; //unique value (text) for each employee
static double Salary; // value of 1500 pounds
char Gender; //character holds f or m
int Available_vacations; //initially starts with 15 days
static double Deduction_per_day; // value of 85.5 pounds
int Available_permission_hours; //initially starts with 20 hours
static double Deduction_per_hour; // value of 15.5 pounds
double Actual_salary; // value with actual salary after deductions
int Vacations; // vacations employee took
int Permessions; // permession hours employee took
int empSerialNum; // object order in vector
department* myDepartment;
vector < project > empProjects;
public:
employee (); // default constructor
employee (string myName, string myNationalID, char myGender,int mySerialNum); // Parameterized constructor
~employee(); // Destractor
//Setters
void setName(string myName);
void setNationalID (string myNationalID);
void setGender (char myGander);
void setAvailableVacation(int myAvVac);
void setAvailablepermissionhours (int myAvPerHours);
void setActualSalary (double actualSalary);
void setVacations(int myVacations);
void setPermessions(int myPermessions);
void setempSerialNum(int mySerialNum);
void setDepartment(department*);
void addProject(project);
//Getters
string getName();
string getNationalID ();
char getGender ();
int getAvailableVacation();
int getAvailablepermissionhours ();
double getActualSalary ();
int getVacations ();
int getPermessions ();
int getempSerialNum();
department* getDepartment();
project* getProjects();
void view (); // View to view Name, ID and actual salary
void View_Detailed (); //call previous function and also shows other details (vacations - permissions - detailed deductions - ... )
void Free_All(); //return all values to default
double Take_vacation(); //this function takes number of days employee need to take as vacation, available vacations reduced by number of days given, if available vacations became 0 salary is deduced by deduction per day set
double Take_permession(); //this function takes hours that employee asked to take, reduce available permission hour by hours given, if available permission become 0 hour salary is reduced by deduction per ho
double Calculate_Actual_Salary();// calculates salary after deductions and returns it
};
#endif
\ifndef员工_
#定义雇员_
使用名称空间std;
#包括
#包括
#包括“department.h”
#包括“project.h”
班级部;
班级项目;
//#包括
雇员
班级员工
{
字符串名称;//带空格的文本
string National_ID;//每个员工的唯一值(文本)
静态双薪;//价值1500英镑
字符性别;//字符包含f或m
int Available_vacations;//最初从15天开始
每天静态双重扣减;//价值85.5磅
int Available_permission_hours;//最初从20小时开始
每小时静态双扣;//值为15.5磅
双倍实际工资;//扣除后的实际工资值
int休假;//员工休假
int permissions;//员工的置换时间
int empSerialNum;//向量中的对象顺序
部门*我的部门;
向量emp项目;
公众:
employee();//默认构造函数
employee(字符串myName,字符串MyNational,字符myGender,int mySerialNum);//参数化构造函数
~employee();//析构函数
//二传手
void setName(字符串myName);
void setnational(字符串mynational);
void setGender(char myGander);
void setavailablevac(int myAvVac);
void setAvailablepermissionhours(整数myAvPerHours);
void setActualSalary(双actualSalary);
作废setVacations(int myVacations);
void setpermissions(int mypermissions);
void setempSerialNum(int mySerialNum);
部门(部门);;
项目(项目);
//吸气剂
字符串getName();
字符串getNationalID();
chargetgender();
int getAvailableVacation();
int getAvailablepermissionhours();
双getActualSalary();
int getVacations();
int getpermissions();
int getempSerialNum();
部门*getDepartment();
project*getProjects();
void view();//查看姓名、ID和实际工资的视图
void View_Detailed();//调用上一个函数并显示其他详细信息(休假-权限-详细扣减-…)
void Free_All();//将所有值返回默认值
double Take_vacation();//此函数以员工需要休假的天数为准,可用假期按给定天数减少,如果可用假期为0,则按每天设置的扣减额扣除工资
double Take_permission();//此函数占用员工要求占用的小时数,按给定的小时数减少可用权限小时数,如果可用权限变为0小时,则按每人扣减工资
double Calculate_Actual_Salary();//计算扣除后的薪资并返回
};
#恩迪夫
h部
#ifndef DEPARTMENT_H_
#define DEPARTMENT_H_
using namespace std;
#include <string.h>
#include "employee.h"
#include "project.h"
#include <vector>
class project;
class employee;
class department{
private:
string name;
string ID;
employee headOfDepartment;
vector <project> depprojects; //projects managed by the department
public:
//constructors
department();
department(string, string);
//Setters
void setName(string);
void setID(string);
void setHeadOfDepartment( employee /*const&*/ depEmployee);
void addProject(project);
//Getters
string getName();
string getID();
employee getHeadOfDepartment() /*const*/;
// project getProjects();
};
#endif
\ifndef部门_
#定义部门_
使用名称空间std;
#包括
#包括“employee.h”
#包括“project.h”
#包括
班级项目;
班级员工;
班级部{
私人:
字符串名;
字符串ID;
部门员工负责人;
vector depprojects;//由部门管理的项目
公众:
//建设者
部门();
部门(字符串,字符串);
//二传手
void setName(字符串);
void setID(字符串);
作废部门设置负责人(员工/*施工人员和*/非员工);
项目(项目);
//吸气剂
字符串getName();
字符串getID();
员工getHeadOfDepartment()/*const*/;
//project getProjects();
};
#恩迪夫
项目.h
#ifndef PROJECT_H_
#define PROJECT_H_
#include <string.h>
#include "department.h"
#include "project.h"
class department;
class project{
string name;
department* location;
public:
//constructors
project();
project(string proName, department* proDepartment);
//Setters
void setName(string proName);
void setLocation(department* proDepartment);
//Getters
string getName();
department* getLocation();
};
#endif
\ifndef项目_
#定义项目_
#包括
#包括“department.h”
#包括“project.h”
班级部;
班级项目{
字符串名;
部门*地点;
公众:
//建设者
项目();
项目(字符串名称,部门*生产部门);
//二传手
void setName(字符串proName);
无效设置位置(部门*生产部门);
//吸气剂
字符串getName();
部门*getLocation();
};
#恩迪夫
您没有在部门类标题中包含定义员工的标题,但是您的头中有一个类型为employee的非引用非指针声明。您需要包括头文件,在头文件中声明了employee
,并且在将其用作对象的类型名称之前,应先定义部门的源文件。另外,我建议为getter添加限定符const,实际上我在其他两个文件中都包含了employee.h,但仍然不起作用。为什么要为类employee
和类项目
进行转发声明?移除它们-这是你的问题。如果使用前向声明,则只能在代码中引用指向类的指针。转发声明只应用于解决不能包含类的头文件的循环依赖性问题。您似乎不需要它们。当我删除它们时,会出现错误“'employee'没有命名类型”好的,您确实有循环依赖关系,因为employee.h
包括department.h
和department.h
包括employee.h
。您需要将提交声明放回原位,并将部门中对员工的所有引用更改为员工
。对于正向声明,编译器不知道对象的大小,因此只能使用指向对象的指针。完全正确!非常感谢。我确实把它改成了其他类中的指针——正如你们所看到的——但我认为如果我使用指针,“组合”在这里不会作为一个原则应用。现在我知道了原因,这似乎是唯一的办法。