“无法分配抽象类型的对象”错误 [英] "Cannot allocate an object of abstract type" error

查看:186
本文介绍了“无法分配抽象类型的对象”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误在这里:

  vector< Graduate *>毕业生
graduates.push_back(new AliceUniversity(identifier,id,salary,average));

祖父母类:

  Graduate :: Graduate(char identifier,
long id,
int salary,
double average)
:_identifier(identifier),
_id (id),_工资(工资),
_average(平均)
{
}

父类:

  UniversityGraduate :: UniversityGraduate(char identifier,
long id,
int salary,
double average)
:毕业(标识符,ID,工资,平均值)
{
}
pre>

实际/小孩类:

  AliceUniversity :: AliceUniversity char标识符,
long id,
int salary,
double average)
:Uni versityGraduate(identifier,id,salary,average)
{
_graduateNum ++;
_sumOfGrades + = average;
_avrA = getAverage();
}

我知道这是一个漫长的镜头,我不能在这里写整个代码...

解决方案

在C ++中,一个至少有一个纯虚函数称为抽象类。您不能创建该类的对象,但可能只有指针或引用。



如果您从抽象类派生,请确保覆盖并定义您的课程的所有纯虚拟功能。



从您的代码段您的课程 AliceUniversity 似乎是一个抽象类。它需要覆盖并定义所有的类$ Graduate UniversityGraduate 的纯虚函数。



纯虚拟函数是声明结束时的 = 0; 的虚拟函数。



示例: virtual void doSomething()= 0;



对于具体答案,您将需要发布您收到错误的类的定义以及该类派生的类。


Error is here:

vector<Graduate *> graduates;
graduates.push_back(new AliceUniversity(identifier,id,salary,average));

Grandparent class:

Graduate::Graduate(char identifier,
                   long id,
                   int salary,
                   double average)
    : _identifier(identifier),
      _id(id),_salary(salary),
      _average(average)
{
}

Parent class:

UniversityGraduate::UniversityGraduate(char identifier,
                                       long id,
                                       int salary,
                                       double average)
    : Graduate(identifier,id,salary,average)
{
}

Actual/child class:

AliceUniversity::AliceUniversity(char identifier,
                                 long id,
                                 int salary,
                                 double average)
    : UniversityGraduate(identifier,id,salary,average)
{
    _graduateNum++;
    _sumOfGrades += average;
    _avrA = getAverage();
}

I know it's a long shot, I cant write the entire code here…

解决方案

In C++ a class with at least one pure virtual function is called abstract class. You can not create objects of that class, but may only have pointers or references to it.

If you are deriving from an abstract class, then make sure you override and define all pure virtual functions for your class.

From your snippet Your class AliceUniversity seems to be an abstract class. It needs to override and define all the pure virtual functions of the classes Graduate and UniversityGraduate.

Pure virtual functions are the ones with = 0; at the end of declaration.

Example: virtual void doSomething() = 0;

For a specific answer, you will need to post the definition of the class for which you get the error and the classes from which that class is deriving.

这篇关于“无法分配抽象类型的对象”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆