我在代码中有一个错误,错误是:(智能感知:不允许抽象类类型"HourlyWorker"的对象:)
I have an error in the code and the error is: (IntelliSense: object of abstract class type "HourlyWorker" is not allowed: )
Employee class是抽象类,因为它包含两个虚函数。PieceWorker 类是一个具体的类,公开继承 Employee 类。HourlyWorker 类是一个具体的类,公开继承 Employee 类。
编译器让我这样做:员工* PTR1;PTR1 = 新计件工人(工资,件(;
但是编译器不允许我在同一代码中执行以下操作:员工* PTR2;ptr2 = 新的小时工(3.3, 6.6(;
以下是供参考的代码:
#include <iostream>
#include <string>
using namespace std;
class Employee
{
char name[30];
char type[30];
public:
void setName() {cin.getline(name,30);}
void setType() {cin.getline(type,30);}
virtual void calculate() = 0;//pure virtual function
virtual void getWage() = 0;//pure virtual function
void getName() {
int index =0;
do
{
cout << name[index] ;
index++;
}
while (name[index]!=' ') ;
cout << endl ;
}
void getType() {
int index =0;
do
{
cout << type[index] ;
index++;
}
while (type[index]!=' ') ;
cout << endl ;
}
};
class PieceWorker: public Employee
{
float wage, totalPay;
int pieces;
public:
PieceWorker() {}
PieceWorker(float wage, int pieces) {
this->wage = wage ;
this->pieces = pieces ;
}
void calculate() {
totalPay = wage * pieces;
}
void getWage()
{
cout << "The total Earning is: " << totalPay << endl ;
}
};
class HourlyWorker: public Employee
{
float wage, hours, totalPay;
public:
HourlyWorker() {}
HourlyWorker(float wage, float hours) {
this->wage = wage ;
this->hours = hours ;
}
void calculte () {
if (hours>40)
{
float extraHours = hours - 40;
float pay1 = extraHours * 1.5 * wage ;
float pay2 = 40 * wage ;
totalPay = pay2 + pay1 ;
}
else
{
totalPay = hours * wage ;
}
}
void getWage()
{
cout << "The total Earning is: " << totalPay << endl ;
}
};
void function(Employee* ptr2);
void main() {
Employee* ptr1;
Employee* ptr2;
cout << "What type of Employee is it: " ;
cout << "1) Piecewise workern2) Hourly Worker ";
int option;
cin >> option;
switch (option)
{
case 1:
cout << "What is the number of pieces made: " ;
int pieces;
cin >> pieces;
cout << "What is the wage per piece: " ;
float wage;
cin >> wage ;
ptr1 = new PieceWorker(wage,pieces);
ptr1->setName();
ptr1->setType();
ptr1->calculate();
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl ;
ptr1->getName();
ptr1->getType();
ptr1->getWage();
delete [] ptr1 ;
break;
case 2:
function(ptr2);
break;
};
system("pause"); }
void function(Employee* ptr2)
{
ptr2 = new HourlyWorker();//this is where the error is occuring
}
您有拼写错误 - calculte
. 为避免这种情况,请使用 override 关键字,然后编译器会警告您不匹配。
void calculate () override {
您的 main 函数返回void
,而它应该返回int
。
相关文章:
- 抽象类错误,请参阅声明" "是抽象的
- C++:处理抽象类中的错误时出现问题
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 与 std::unique_ptr 和抽象类关联的编译错误
- 无法实例化抽象类错误
- 错误 - 变量类型 "X" 是一个抽象类 - c++
- 错误消息:不允许抽象类类型 "X" 的对象:纯虚拟"Y"没有覆盖器
- 错误:不允许使用抽象类类型的对象
- 我在代码中有一个错误,错误是:(智能感知:不允许抽象类类型"HourlyWorker"的对象:)
- 如何修复错误C2259:无法实例化抽象类
- 在另一个项目中包括继承的类时,抽象类的链接器错误
- 使用从WINRT网格类派生的抽象类时,Casttobase未解决的外部符号错误
- 抽象类和唯一指针 c++ 错误
- 从另一个抽象类得出一个抽象类时,C 中的类重新定义错误
- 使用std :: make_shared抽象类实例化的错误
- C++ Qt 5 中构造函数的抽象类错误
- C++,无法实例化抽象类错误
- 抽象类出现C++ILink32错误
- 错误 C2259:'CDocument':无法实例化抽象类
- 帮助错误 C2259:无法实例化抽象类