不能将变量't'声明为抽象类型'AddSalariedEmployee'

时间:2017-11-27 04:55:02

标签: c++ build constructor abstract-class

我在C ++中编译时遇到问题。请帮帮我!

Test.cpp的

#include <gtest/gtest.h>
#include "../PayRoll/AddSalariedEmployee.h"
#include "../PayRoll/Employee.h"

class GregorianCalendar;

class PayrollTest : public ::testing::Test {
protected:
    virtual void TearDown() {
    }
    virtual void SetUp() {
    }

public:
    PayrollTest() : Test() {
        int emId = 1;
        AddSalariedEmployee t(emId, "Bod", "Home", 1000.00);
        t.Execute();

    }
    GregorianCalendar* gregorian_calendar;
};


TEST(PayrollTest, check_name_employee) {
    Employee* e = GpayrollDatabase.GetEmployee(1);
    EXPECT_EQ("Bob", e->GetName());
}

AddSalariedEmployee.h

我认为这个文件存在问题

//
// Created by lngo9 on 11/24/2017.
//

#ifndef PAYROLL_ADDSALARIEDEMPLOYEE_H
#define PAYROLL_ADDSALARIEDEMPLOYEE_H

#include "AddEmployeeTransaction.h"

#include <string>
using namespace std;

class AddSalariedEmployee : public AddEmployeeTransaction {
    public:
        virtual ~AddSalariedEmployee();
        AddSalariedEmployee(int empid, string name, string address, double salary);
        PaymentClassification* GetClassification() const;
        PaymentSchedule* GetSchedule() const;

    private:
        double itsSalary;

};


#endif //PAYROLL_ADDSALARIEDEMPLOYEE_H

AddSalariedEmployee.cpp

//
// Created by lngo9 on 11/24/2017.
//

#include "AddSalariedEmployee.h"
#include "SalariedClassification.h"
#include "MonthlySchedule.h"

AddSalariedEmployee::~AddSalariedEmployee() {
}

AddSalariedEmployee::
AddSalariedEmployee(int empid, string name, string address, double salary)
        : AddEmployeeTransaction(empid, name, address)
        , itsSalary(salary){}

PaymentClassification*
AddSalariedEmployee::GetClassification() const {
    return new SalariedClassification(itsSalary);
}

PaymentSchedule* AddSalariedEmployee::GetSchedule() const {
    return new MonthlySchedule();
}

AddEmployeeTransaction.h

//
// Created by lngo9 on 11/24/2017.
//

#ifndef PAYROLL_ADDEMPLOYEETRANSACTION_H
#define PAYROLL_ADDEMPLOYEETRANSACTION_H

#include "Transaction.h"
#include "PayrollDatabase.h"
#include <string>

using namespace std;
class PaymentClassification;
class PaymentSchedule;

extern PayrollDatabase GpayrollDatabase;

class AddEmployeeTransaction : public Transaction {
    public:
        virtual ~AddEmployeeTransaction();
        AddEmployeeTransaction(int empid, string name, string address);
        virtual PaymentClassification* GetClassification() const = 0;
        virtual PaymentSchedule* GetSchedule() const = 0;
        virtual void Execute() = 0;

    private:
        int itsEmpid;
        string itsName;
        string itsAddress;

};


#endif //PAYROLL_ADDEMPLOYEETRANSACTION_H

AddEmployeeTransaction.cpp

//
// Created by lngo9 on 11/24/2017.
//

#include "AddEmployeeTransaction.h"
#include "HoldMethod.h"
#include "Employee.h"

class PaymentMethod;
class PaymentSchedule;
class PaymentClassification;



AddEmployeeTransaction::~AddEmployeeTransaction() {
}

AddEmployeeTransaction::
AddEmployeeTransaction(int empid, string name, string address)
        : itsEmpid(empid)
        , itsName(name)
        , itsAddress(address){}

PaymentClassification* AddEmployeeTransaction::GetClassification() const {}

PaymentSchedule* AddEmployeeTransaction::GetSchedule() const {}

void AddEmployeeTransaction::Execute() {
    PaymentClassification* pc = GetClassification();
    PaymentSchedule* ps = GetSchedule();
    PaymentMethod* pm = new HoldMethod();
    Employee* e = new Employee(itsEmpid, itsName, itsAddress);
    e->SetClassification(pc);
    e->SetSchedule(ps);
    e->SetMethod(pm);
    GpayrollDatabase.AddEmployee(itsEmpid, e);

}

Transaction.h

//
// Created by lngo9 on 11/24/2017.
//

#ifndef PAYROLL_TRANSACTION_H
#define PAYROLL_TRANSACTION_H


class Transaction {
    public:
        virtual void Execute();
};


#endif //PAYROLL_TRANSACTION_H

构建时我收到错误:  错误:无法将变量't'声明为抽象类型'AddSalariedEmployee'          AddSalariedEmployee t(emId,“Bod”,“Home”,1000.00);

请帮帮我!非常感谢!

1 个答案:

答案 0 :(得分:0)

AddSalariedEmployeeAddEmployeeTransaction的子项,它是一个抽象类(您使用= 0作为某些方法的主体)。
由于这些方法没有在dervied类中定义,派生类仍然是抽象的,无法实例化。

您需要定义(实现)所有抽象方法(即使您只给它们一个空体{}),以便能够实例化子类。