错误C2259:“< someclass>” :无法实例化抽象类 [英] error C2259: '<someclass>' : cannot instantiate abstract class

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

问题描述

在尝试编译VS 6 VC ++代码时遇到此编译器错误。 < someclass> 不是抽象类。当点击错误时,指针指向函数第一行的列表系统文件

Am getting this compiler error while trying to compile an VS 6 VC++ code. The <someclass> is not an abstract class. And when clicked on the error the pointer points to list system file at the first line of the function

void resize(size_type _Newsize, _Ty _Val)       
{     
     if (_Mysize < _Newsize)  
         _Insert_n(end(), _Newsize - _Mysize, _Val);  
     else  
     while (_Newsize < _Mysize)  
        pop_back();  
}

奇怪。任何解决方案。 Class,

Strange. Any solutions. Class ,

class SomeClass: public parentObject
{

  public:
    SomeClass() {}
    SomeClass(const someotherclass& p, double uu, double vv)
        { z= p; u = uu; v = vv; }
protected:
    double      u, v;       
     someotherclass z;  
};


推荐答案

如果具有任何纯虚拟成员。这些纯虚拟成员是在该类中还是在基类中声明的。除非所有成员都存在,否则无法创建类。具有纯虚拟成员(无论是在类中还是在基类中声明)的类称为抽象类。

You cannot create an instance of a class if it has any pure-virtual members. Whether those pure-virtual members were declared in that class or in a base class. A class cannot be created unless all of its members exist. A class that has pure-virtual members (whether declared in the class or in a base class) is called an abstract class.

因此,如果您从基类继承,具有纯虚拟成员,如果要创建该类的实例(例如将它们放入 std :: vector 必须在派生类中实现这些成员$ c>)。

So if you inherit from a base class that has pure-virtual members, you must implement these in the derived class if you want to create instances of that class (like putting them in a std::vector).

您可以将指向抽象类的指针放在 std :: vector 中。但是只有指针,而不是对象本身。因此,您需要使用 new 分配对象,但是由于该类是抽象的,因此根本无法创建它们。因此,您将需要派生一个实现纯虚拟方法的新类。然后,您将能够创建该类并将其放入您的 std :: vector< someclass *>

You can put pointers to an abstract class in a std::vector. But only pointers, not the object themselves. So you would need to allocate your objects with new, but since the class is abstract you cannot create them at all. So you will need to derive a new class that implements the pure-virtual methods. And then you will be able to create that class and put it in your std::vector<someclass*>.

这篇关于错误C2259:“&lt; someclass&gt;” :无法实例化抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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