【问题标题】:C++: Derived classes, "no matching constructor" errorC++:派生类,“没有匹配的构造函数”错误
【发布时间】:2016-04-21 19:00:48
【问题描述】:

我从事这项任务已经有一段时间了。以下是说明:

你要设计一个名为 Employee 的抽象类,它的成员是 如下所示(使它们受到保护):

数据成员:char *name, long int ID

两个构造函数:一个默认构造函数 // 将数据成员初始化为 默认值和复制构造函数

方法: setPerson (char *n, long int id) //允许用户设置 每个人的信息 一个名为 Print () 的函数 // 应该是 虚函数,打印类的数据属性。和一个 析构函数

还定义了两个派生自 Employee 类的类,称为 经理兼秘书。每个类都应该继承所有成员 基类,也有自己的数据成员和成员函数。 经理应该有一个数据成员,称为他/她的学位 本科学位(例如文凭、学士、硕士、博士)、 秘书应该有她的合同(可以是布尔值 1/0 永久/临时)。

派生类的所有成员函数都应该从它们的 基类。

编写以下 main() 来测试你的类

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 行(主代码的第一行)出现错误:

Manager 的初始化没有匹配的构造函数

我尝试阅读类似的问题,但它们对我的帮助不大。我认为最令人困惑的是合同是布尔值和 char 参数的使用。任何指导都表示赞赏。

【问题讨论】:

  • 第 57 行和第 62 行的错误P.Print(); 并没有给出您提到的错误。他们更有可能给出left of '.Print' must have class/struct/union 错误消息。
  • @RawN 我的第 62 行与这里的第 62 行不同(因为 Xcode 在开头添加了 cmets。)这就是为什么我指定它是主代码的第一行,但你是正确的打印功能也不起作用。我没有提到它,因为我认为这是 Manager 未初始化的错误。

标签: c++ class derived-class


【解决方案1】:

您遇到的错误非常简单:您没有任何将字符串、整数 (?) 和字符串作为参数的 Manager(或 Employee)构造函数。

【讨论】:

  • 我应该使用 char * 和 int 作为参数。这就是让我困惑的地方。另外,我将所有字符都更改为字符串,但它仍然给我同样的错误。
  • @hopillium 您是否向 Manager 添加了“Manager(...)”构造函数?
【解决方案2】:

您已经声明了构造函数雇员,但没有定义它。 看看类员工,在你声明的 publc 下

Employee();
Employee(Employee&);

但是你还没有定义函数。你需要

Employee :: Employee()
{
    bla bla bla
}

另一个签名。

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 2020-06-09
    相关资源
    最近更新 更多