我清楚地初始化了我的课程,但我收到的错误是说我没有

时间:2016-04-19 15:53:38

标签: c++

  

error1-error C2259:'HourlyEmployee':无法实例化抽象类
  不允许使用抽象类类型“HourlyEmployee”的error2-object:
   纯虚函数“Employee :: printPay”没有覆盖

此错误属于此部分,我不明白这个问题。

VE.push_back(new HourlyEmployee(empID, hours, payRate));

main.cpp中:

#include "hourly.h"
#include "lab7.h"
#include "salarie.h"
#include <iostream>
#include <iomanip>
#include <vector>

void getInput(vector <Employee *> & VE);
void printList(const vector <Employee *> & VE);
using namespace std;
int main()
{
    vector <Employee *> VEmp;
    getInput(VEmp);
    printList(VEmp);
    cout << "Lab 7 completed by: " << "youssef zaki " << endl;
    system("pause");
    return 0;
}

void getInput(vector<Employee*>& VE)
{

    std::vector < HourlyEmployee > ;
        int empID,
            choice;
        double salary,
                hours,
                payRate;
        do
        {
            cout << "Enter 1 for Hourly Employee" << endl;
            cout << "Enter 2 for Salaried Employee" << endl;
            cout << "Enter 3 to stop:  ";

            cin >> choice;
            cout << endl;

            // process option
            switch (choice)
            {
            case 1:
                cout << "Enter the ID: ";
                cin >> empID;
                cout << "Enter the number of hours worked: ";
                cin >> hours;
                cout << "Enter the pay rate: ";
                cin >> payRate;         
                VE.push_back(new HourlyEmployee(empID, hours, payRate));
                break;
            case 2:
                cout << "Enter the ID: ";
                cin >> empID;
                cout << "Enter the salary: ";
                cin >> salary;
                VE.push_back(new SalariedEmployee(empID, salary));
                break;
            case 3:
                break;
            default:
                cout << "Error: invalid option" << endl;
            }
            cout << endl;
        } while (choice != 3);

}

void printList(const vector<Employee*>& VE )
{
    for (std::size_t i = 0; i != VE.size(); ++i)
    {
        VE[i]->printPay();
    }
}

employ.h文件:

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

class Employee
{
private:
    int EmpID;
public:
    Employee(int);
    ~Employee();
    int getEmpID() const;
    virtual void printPay()=0;
};

#endif

employ.cpp:

#include "lab7.h"
#include <iostream>
#include <iomanip>
#include <vector>

Employee::Employee(int empID)
{
    EmpID = empID;
}
Employee::~Employee()
{
}
int Employee::getEmpID() const
{
    return EmpID;
}

employalarie.h文件:

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

class SalariedEmployee: public Employee
{
private:
    double salary;

public:
    SalariedEmployee(int, double);
    ~SalariedEmployee();
    double getSalary() const;
    void printPay();
};
#endif

employsalry.cpp:

#include "salarie.h"
#include <vector>

SalariedEmployee::SalariedEmployee(int empID, double salary) : `         `   Employee(empID)
{
    salary = salary;
}

SalariedEmployee::~SalariedEmployee()
{
}

double SalariedEmployee::getSalary() const
{
    return salary;
}

void SalariedEmployee::printPay()
{
    double weeklyPay = salary / 52;
    cout << fixed << showpoint << setprecision(2);
    cout << "The pay for the salaried employee with ID number ";
    cout << getEmpID() << " is  $" << weeklyPay << endl;
}

hourlyemploy.h:

#ifndef HOURLYEMPLOYEE_H
#define HOURLYEMPLOYEE_H

#include "lab7.h"
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

class HourlyEmployee : public Employee
{
private:
    double hours;
    double payRate;

public:
    HourlyEmployee(int, double, double);
    ~HourlyEmployee();
    double getHours() const;
    double getPayRate() const;
    void printPay(double);
};
#endif 

hourlyemploy.cpp:

#include "hourly.h"
#include <iostream>
#include <iomanip>
#include <vector>
HourlyEmployee::HourlyEmployee(int empID, double hours, double payRate)
    : Employee(empID)
{
    this->hours = hours;
    this->payRate = payRate;
}
HourlyEmployee::~HourlyEmployee()
{
}
double HourlyEmployee::getHours() const
{
    return hours;
}
double HourlyEmployee::getPayRate() const
{
    return payRate;
}
void HourlyEmployee::printPay(double)
{
    double weeklyPay = hours * payRate;
    cout << fixed << showpoint << setprecision(2);
    cout << "The pay for the hourly employee with ID number ";
    cout << getEmpID() << " is  $" << weeklyPay << endl;
}

1 个答案:

答案 0 :(得分:0)

错误消息为您提供所有信息:

  

error1-error C2259:'HourlyEmployee':无法实例化抽象类error2-不允许抽象类类型为“HourlyEmployee”的对象:   纯虚函数“Employee :: printPay”没有覆盖

在HourlyEmployee类中,您没有覆盖void printPay(),您只添加了相同名称但不同参数列表的方法:void printPay(double);

我建议在覆盖虚拟方法时使用override关键字:

void printPay(double) override; // this will generate error (signature mismatch)

,同时:

void printPay() override; // will work just fine

您的代码中的另一个问题:

SalariedEmployee::SalariedEmployee(int empID, double salary) : `         `   Employee(empID)
{
    salary = salary;
}

这里薪水是SalariedEmployee中的一个局部变量,用于正确初始化类变量use

this->salary = salary;

获得额外积分的更多提示(来自您的HW): - )

  1. 请勿使用using namespace std;
  2. 请勿使用endl,而应使用"\n"
  3. 不要使用原始指针,使用std::unique_ptr<>,如果需要共享,请std::shared_ptr<>。这两者与std::make_uniquestd::make_shared一起。
  4. 在您的代码中使用new,但我从未找到delete - 那就是要求内存泄漏。