gpt4 book ai didi

c++ - 无法将变量 't' 声明为抽象类型 'AddSalariedEmployee'

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:05 24 4
gpt4 key购买 nike

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

测试.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);

}

交易.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);

请帮帮我!非常感谢!

最佳答案

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

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

关于c++ - 无法将变量 't' 声明为抽象类型 'AddSalariedEmployee',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47504163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com