ERROR1錯誤C2259:「HourlyEmployee」:不能實例化抽象類抽象類類型的
誤差2-對象「HourlyEmployee」是不允許的:
純虛函數「員工:: 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;
}
employsalarie.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;
}
'不能實例化抽象類'似乎足夠清晰... –
該錯誤消息從字面上做了一切,但實際上爲您修復了代碼。 – WhozCraig