C++:派生类, "no matching constructor"错误

标签 c++ class derived-class

我已经为这项任务工作了一段时间。以下是说明:

You are to design an abstract class called Employee whose members are as given below (make them protected):

Data members: char *name, long int ID

Two constructors: A Default constructor // intitialize data memebrs to the default values and a copy constructor

Methods: setPerson (char *n, long int id) //allows user to set information for each person A function called Print () // should be a virtual function, that prints the data attributes of the class. and a destructor

Also define two classes that derived from class Employee, called Manager and Secretary. Each class should inherit all members from the base class and has its own data members and member functions as well. The Manager should have a data member called degree for his/her undergraduate degree (e.g. diploma, bachelor, master, doctor), the Secretary should have her contract (can be a Boolean value 1/0 for permanent/temporary).

All member functions of derived class should be overrided from their base class.

Write the following main() to test your classes

int main() {
Employee * p = new Manager(“Bruce Lee”, 0234567, “Dr.”);
P.print();
Secretary p2;
p2.setPerson(“Wilma Jones”, 0341256, “permanent”);
delete p;
p = & p2;
p.Print();
return 0;
}

这是我到目前为止所想出的一切,但我很确定它充满了错误,而且我的参数和变量类型都不对。

#include <iostream>
using namespace std;


class Employee{
protected:
    char *name;
    long int ID;
public:
    Employee();
    Employee(Employee&);
    void setPerson(char * n, long int eID) {
        name = n;
        ID = eID; };
    virtual void Print(){
        cout << "Name: " << name << endl;
        cout << "ID: " << ID << endl; };
};

class Manager: public Employee {
protected:
    char *degree;
public:
    void setPerson(char * n, long int eID, char * d){
        name = n;
        ID = eID;
        degree = d;
    };
    void Print() {
        cout << "Name: " << name << endl;
        cout << "ID: " << ID << endl;
        cout << "Degree: " << degree << endl;
    };
};

class Secretary: public Employee {
protected:
    bool contract;
public:
    void setPerson(char * n, long int eID, string c){
        name = n;
        ID = eID;
        if (c == "permanent") contract = true;
        else contract = false;
    };
    void Print(){
        cout << "Name: " << name << endl;
        cout << "ID: " << ID << endl;
        cout << "Contract: " << contract << endl;
    };
};

int main() {
    Employee * P = new Manager("Bruce Lee", 0234567, "Dr.");
    P.Print();
    Secretary P2;
    P2.setPerson("Wilma Jones", 0341256, "permanent");
    delete P;
    P = & P2;
    P.Print();
    return 0;
}

我在第 62 行(主要代码的第一行)出现错误:

No matching constructor for initialization of Manager

我试过阅读类似的问题,但它们对我帮助不大。我认为最令人困惑的事情是契约(Contract)是一个 bool 值和 char 参数的使用。任何指导都表示赞赏。

最佳答案

你得到的错误非常简单:你没有任何Manager(或Employee)的构造函数接受一个字符串,整数(? ), 和字符串作为参数。

关于C++:派生类, "no matching constructor"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36778291/

相关文章:

加载jar文件时找不到Java类

C++ 维护子类对象的混合集合

java - 从 java 调用编译的 C++ exe 文件不起作用

c++ - 我想以特定方式在C++中声明类似金字塔的结构,但是不能

c++ - 在 Eclipse 中运行多个 Qt 测试

javascript - 如何根据字符串值实例化对象

c++ - 在函数参数中分配/初始化引用

c++ - 在匿名类中返回* this时, 'auto'返回类型是什么类型?

c++ - C++ 中基类和派生类的作用域

C++从基类指针访问派生类成员