没有用于调用employee :: employee()的匹配函数

cxf*_*f54 1 c++ derived-class

我正在尝试TCPL的派生类示例.

经理是一名员工,有额外的级别信息.我一直收到错误:

no matching function for call to employee::employee() 
in the constructor of manager::manager()
Run Code Online (Sandbox Code Playgroud)

所有员工都是公开的,可访问的.经理构造函数有什么问题?

#include <iostream>
#include <string>
using namespace std;


class employee{
  public:
    enum type {M,E};
    type hier;
    string id;
    employee(string idd):hier(E){id=idd;};
};

class manager: public employee{
  public:
    int level;
    manager(string idd, int lv){hier=employee::M; id=idd;level=lv;};
};

void print(employee *x){
  if(x->hier == employee::E){
    cout<<"I am employee with id "<<x->id<<endl;
}
  else{
    cout<<"I am manager with id "<<x->id<<endl;
    manager* y=static_cast<manager *>(x);
    cout<<"My level is "<<y->level<<endl;
  }
}

int main(){
  employee t("334");
  manager u("223", 2);
  print(&t);
  print(&u);
}
Run Code Online (Sandbox Code Playgroud)

第二个版本 以前的版本在封装方面很糟糕

这是新版本

#include <iostream>
using namespace std;

enum type {M, E};

class employee{
  string id;
  type hier;
  public:
    employee(string idd, type hi=E):hier(hi),id(idd){}
    string whatid(){return id;}
    type whattype(){return hier;}
};

class manager: public employee{
  int level;
  public:
    manager(string idd, int lv):employee(idd,M),level(lv){}
    int whatlv(){return level;}
};
Run Code Online (Sandbox Code Playgroud)

我没有直接访问员工和经理的私人成员,而是将成员设为私有,并使用函数来访问它们.

#include <iostream>
#include <string>
#include "manager_employee.h"
using namespace std;

void print(employee *x){
  if(x->whattype() == E){
    cout<<"I am employee with id "<<x->whatid()<<endl;
  }
  else{
    cout<<"I am manager with id "<<x->whatid()<<endl;
    manager *y=(manager *)x;
//    manager* y=static_cast<manager *>(x);
    cout<<"My level is "<<y->whatlv()<<endl;
  }
}

int main(){
  employee t("334", E);
  manager u("223", 2);
  print(&t);
  print(&u);
}
Run Code Online (Sandbox Code Playgroud)

Mik*_*our 5

通过声明构造函数employee,删除其默认构造函数; 所以你不能在没有指定ID的情况下创建一个.

这意味着任何派生类都需要为基类构造函数提供ID:

manager(string idd, int lv) : employee(idd) {hier=employee::M; level=lv;}
//                            ^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

为了保持一致性,您可能希望level在初始化列表而不是构造函数体中初始化; hier通过employee构造函数中的另一个参数初始化为正确的值可能更有意义,而不是给它一个默认值然后覆盖它:

employee(string idd, type hier = E) : hier(hier), id(idd) {}
manager(string idd, int lv) : employee(idd, M), level(lv) {}
Run Code Online (Sandbox Code Playgroud)