2016-04-19 20 views
-2

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; 
} 
+5

'不能實例化抽象類'似乎足夠清晰... –

+4

該錯誤消息從字面上做了一切,但實際上爲您修復了代碼。 – WhozCraig

回答

0

錯誤信息給你所有的信息:

ERROR1誤差C2259:「HourlyEmployee」:不能實例抽象類類型的抽象類誤差2-對象「HourlyEmployee」是不允許的: 純虛函數「僱員:: 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 SA局部變量,以正確初始化類變量使用

this->salary = salary; 

更多提示,以賺取額外加分(從你的HW):-)

  1. 不要使用using namespace std;
  2. 待辦事項不使用endl,而是"\n"
  3. 請勿使用原始指針,請使用std::unique_ptr<>並且如果您需要共享它母雞std::shared_ptr<>。這兩個連同std::make_uniquestd::make_shared

在你的代碼中,你使用的是new,但我從來沒有找到delete - 這就要求內存泄漏。

+0

真的很有幫助謝謝 –

相關問題