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

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

问题描述

此处出错:

 矢量<毕业*>毕业生

graduates.push_back(new AliceUniversity(identifier,id,salary,average));

祖父类:

  Graduate :: Graduate(char identifier,long id,int salary,double average):
_identifier(identifier),_ id(id),_ salary(salary),_ average b {

}

{
做一些事情。
}

父级:

  UniversityGraduate :: UniversityGraduate(char identifier,long id,int salary,

double average)

:Graduate ,薪金,平均)
{

}

{
做一些事情。
}

实际值:

  AliceUniversity :: AliceUniversity(char identifier,long id,int salary,double 

average)

:UniversityGraduate ,薪金,平均)

{
_graduateNum ++;
_sumOfGrades + = average;
_avrA = getAverage();
}



我知道它的一个长镜头,我不能写这里的整个代码。 。
如果任何人有一个想法...

解决方案

在C ++中至少有一个 pure虚函数称为抽象类,您无法创建该类的对象,你只能有指针或引用它。



如果你从一个抽象类派生,确保你覆盖和定义所有Pure虚拟函数为你的类。 p>

从你的代码片段你的类 AliceUniversity 似乎是一个抽象类。它需要重写并定义类 Graduate UniversityGraduate 的所有纯虚函数。



纯虚函数是用 = 0;



virtual void doSomething()= 0;



对于一个特定的答案,你需要定义类你会得到错误和类从中导出的类。


Error over here:

vector<Graduate *> graduates

graduates.push_back(new AliceUniversity(identifier,id,salary,average));

Grandfather class:

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

     }

     {
      do some things.
     }

Father class:

    UniversityGraduate::UniversityGraduate( char identifier, long id, int salary, 

     double average)

    :Graduate(identifier,id,salary,average)
    {

    }

    {
    do some things.
    }

Actual calss:

     AliceUniversity::AliceUniversity(char identifier, long id, int salary, double 

     average)

    :UniversityGraduate(identifier,id,salary,average)

     {
    _graduateNum++;
    _sumOfGrades+=average;
    _avrA=getAverage();
     }

I know its a long shot, i cant write the entire code here... if anyone has an idea...

解决方案

In C++ a class with at least one pure virtual function is called Abstract class and you cannot create objects of that class, You can only have pointers or references to it.

If you are deriving from an Abstract class 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. And it needs to override and define all the pure virtual functions of the classes Graduate and UniversityGraduate.

Pure virtual functions are the ones declared with =0;

eg: virtual void doSomething() = 0;

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

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

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