error C2259: '' : cannot instan

2019-02-27 04:15发布

问题:

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();  
}

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;  
};

回答1:

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.

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).

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*>.