我在代码中有一个错误,错误是:(智能感知:不允许抽象类类型"HourlyWorker"的对象:)

I have an error in the code and the error is: (IntelliSense: object of abstract class type "HourlyWorker" is not allowed: )

本文关键字:错误 抽象类 不允许 类型 对象 HourlyWorker 感知 代码 有一个 智能      更新时间:2023-10-16

Employee class是抽象类,因为它包含两个虚函数。PieceWorker 类是一个具体的类,公开继承 Employee 类。HourlyWorker 类是一个具体的类,公开继承 Employee 类。

编译器让我这样做:员工* PTR1;PTR1 = 新计件工人(工资,件(;

但是编译器不允许我在同一代码中执行以下操作:员工* PTR2;ptr2 = 新的小时工(3.3, 6.6(;

以下是供参考的代码:

#include <iostream>
#include <string>
using namespace std;
class Employee
{
    char name[30];
    char type[30];
public:
    void setName() {cin.getline(name,30);}
    void setType() {cin.getline(type,30);}
    virtual void calculate() = 0;//pure virtual function
    virtual void getWage() = 0;//pure virtual function
    void getName() {
        int index =0;
        do
        {
            cout << name[index] ;
            index++;
        }
        while (name[index]!='') ;
        cout << endl ;
    }
    void getType() {
            int index =0;
        do
        {
            cout << type[index] ;
            index++;
        }
        while (type[index]!='') ;
        cout << endl ;
    }
};
class PieceWorker: public Employee
{
    float wage, totalPay;
    int pieces;
public:
    PieceWorker() {}
    PieceWorker(float wage, int pieces) {
        this->wage = wage ;
        this->pieces = pieces ;
    }
    void calculate() {
        totalPay = wage * pieces;
    }
    void getWage()
    {
        cout << "The total Earning is: " << totalPay << endl ;
    }
};
class HourlyWorker: public Employee
{
    float wage, hours, totalPay;
public:
    HourlyWorker() {}
    HourlyWorker(float wage, float hours) {
        this->wage = wage ;
        this->hours = hours ;
    }
    void calculte () {
        if (hours>40)
        {
            float extraHours = hours - 40;
            float pay1 = extraHours * 1.5 * wage ;
            float pay2 = 40 * wage ;
            totalPay = pay2 + pay1 ;
        }
        else
        {
            totalPay = hours * wage ;
        }
    }
    void getWage()
    {
        cout << "The total Earning is: " << totalPay << endl ;
    }
};
void function(Employee* ptr2);
void main() {
    Employee* ptr1;
    Employee* ptr2;
    cout << "What type of Employee is it: " ;
    cout << "1) Piecewise workern2) Hourly Worker";
    int option;
    cin >> option;
    switch (option)
    {
    case 1:
        cout << "What is the number of pieces made: " ;
        int pieces;
        cin >> pieces;
        cout << "What is the wage per piece: " ;
        float wage;
        cin >> wage ;
        ptr1 = new PieceWorker(wage,pieces);
        ptr1->setName();
        ptr1->setType();
        ptr1->calculate();
        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl ;
        ptr1->getName();
        ptr1->getType();
        ptr1->getWage();
        delete [] ptr1 ;
        break;
    case 2:
        function(ptr2);
        break;
    };
    system("pause"); }
void function(Employee* ptr2)
{
    ptr2 = new HourlyWorker();//this is where the error is occuring

}

您有拼写错误 - calculte . 为避免这种情况,请使用 override 关键字,然后编译器会警告您不匹配。

 void calculate () override {

您的 main 函数返回void,而它应该返回int