我有这个错误。 " 错误:无法调用'员工::员工()' "。当我写一个关于"教师班的构造函数"我有这个错误。 你能帮我解决这个问题吗?我求助于:(。 还有2个班级。 "日期"和#34;人"。 "人"是"员工"的超级班。
Employee.h
#ifndef GK_EMPLOYEE_H
#define GK_EMPLOYEE_H
#include "GK_Person.h"
class Employee:public Person
{
public:
Employee(char* name,char* surname,char* sex,char* pob, Date* dob,Date* start,char* title,int salary,bool fulltime =true);// start refers to dynamic memory
//virtual ~Employee();
void SetTitle(char* title);
friend ostream& operator<<(ostream&,const Employee&);
friend class Faculty;
private:
char* m_title;
int m_salary;
bool m_fulltime;
Date* m_start;// date started working
};
#endif // GK_EMPLOYEE_H
Employee.cpp
#include "GK_Employee.h"
Employee::Employee(char* name,char* surname,char* sex,char* pob, Date* dob,Date* start,char* title,int salary,bool fulltime/*=true*/){
m_name=name;
m_surname=surname;
m_sex=sex;
m_pob=pob;
m_dob=dob;
m_start=start;
m_title=title;
m_salary=salary;
m_fulltime=fulltime;
}
void Employee::SetTitle(char* title){
m_title = title;
}
Faculty.h
#ifndef GK_FACULTY_H
#define GK_FACULTY_H
#include "GK_Employee.h"
class Faculty :public Employee
{
public:
Faculty(char* name,char* surname,char* sex,char* pob, Date* dob,Date* start,int level,int salary,Date* dolat,bool fulltime = true);
//virtual ~Faculty();
char GetType() const{return'F';}
void Promote(Date* dt);
friend ostream& operator<<(ostream&,const Faculty&);
private:
Date* m_dolat;// date of last academic title owned
int m_level;// 0 for Dr., 1 for Ass.Prof.Dr., 2 for Assoc.Prof.Dr. and 3 for Prof.Dr.
};
#endif // GK_FACULTY_H
Faculty.cpp
#include "GK_Faculty.h"
Faculty::Faculty(char* name,char* surname,char* sex,char* pob, Date* dob,Date* start,int level,int salary,Date* dolat,bool fulltime/*= true*/){
}
答案 0 :(得分:4)
由于Faculty
来自Employee
,您需要在Employee
构造函数中初始化Faculty
的成员。这必须通过调用Employee
的构造函数来完成。如果Employee
有一个默认构造函数(即没有任何参数的构造函数),则编译器会隐式执行此操作。但是Employee
只定义了以下构造函数:
Employee(char* name,char* surname,char* sex,char* pob, Date* dob,Date* start,char* title,int salary,bool fulltime =true);// start refers to dynamic memory
编译器无法告诉传递给此c'tor的参数,因此您需要自己从Faculty
c'tor内部调用它。像这样:
#include "GK_Faculty.h"
Faculty::Faculty(char* name,char* surname,char* sex,char* pob, Date* dob,Date* start,int level,int salary,Date* dolat,bool fulltime/*= true*/)
: Employee(name,surname,sex,pob,dob,start,"mytitle",salary,fulltime)
{
}
答案 1 :(得分:2)
您的Faculty构造函数需要显式构造Employee,因为它没有默认构造函数:
Faculty::Faculty(char* name, char* surname, char* sex, char* pob, Date* dob, Date* start, int level, int salary, Date* dolat, bool fulltime /*= true*/)
: Employee(name, surname, ....) {
}
答案 2 :(得分:1)
初始化的主题是在C ++中构造对象的一个重要方面。构造类时,必须有一种明确的方法来初始化类的所有基类以及类的所有成员数据。
如果在不使用显式初始化程序的情况下无法初始化任何基类或成员数据,编译器将报告错误,除非您添加代码以在初始化程序列表中初始化它们。
确保在Employee
构造函数的初始化列表中使用带有正确参数的Faculty
构造函数。
更改
Faculty::Faculty(char* name,char* surname,char* sex,char* pob, Date* dob,Date* start,int level,int salary, Date* dolat,bool fulltime/*= true*/) { }
到
Faculty::Faculty(char* name,char* surname,char* sex,char* pob, Date* dob,Date* start,int level,int salary, Date* dolat,bool fulltime/*= true*/) : Employe(name, surname, sex, pob, dob, start, level, salary, dolat, fulltime) { }