How do you handle a “cannot instantiate abstract c

2020-02-24 13:05发布

How do you handle a "cannot instantiate abstract class" error in C++? I have looked at some of the similar errors here and none of them seem to be exactly the same or problem that I am having. But, then again, I will admit that there are several to go over. Here is the compile error:

[IMG]http://i67.photobucket.com/albums/h292/Athono/cannotinstantiateabstractclass.png[/IMG]

This leads me to this page: http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(C2259);k(VS.ERRORLIST)&rd=true Compile Error C2259 is from a C++ program but the page calls the abstract class an "interface":

Whenever you derive from an interface and implement the interface methods in the derived class with access permissions other than public, you may receive C2259. This occurs because the compiler expects the interface methods implemented in the derived class to have public access. When you implement the member functions for an interface with more restrictive access permissions, the compiler does not consider them to be implementations for the interface methods defined in the interface, which in turn makes the derived class an abstract class.

There are two possible workarounds for the problem:

Make the access permissions public for the implemented methods.

Use the scope resolution operator for the interface methods implemented in the derived class to qualify the implemented method name with the name of the interface.

The bad news is that I have already made all of the methods public in the class:

class AmbientOccluder: public Light {
    public:

        AmbientOccluder(void); 

7条回答
趁早两清
2楼-- · 2020-02-24 13:17

Visual Studio's Error List pane only shows you the first line of the error. Invoke View>Output and I bet you'll see something like:

c:\path\to\your\code.cpp(42): error C2259: 'AmbientOccluder' : cannot instantiate abstract class
          due to following members:
          'ULONG MysteryUnimplementedMethod(void)' : is abstract
          c:\path\to\some\include.h(8) : see declaration of 'MysteryUnimplementedMethod'
查看更多
爷的心禁止访问
3楼-- · 2020-02-24 13:17

I have answered this question here..Covariant virtual functions return type problem

See if it helps for some one.

查看更多
看我几分像从前
4楼-- · 2020-02-24 13:22

Why can't we create Object of Abstract Class ? When we create a pure virtual function in Abstract class, we reserve a slot for a function in the VTABLE(studied in last topic), but doesn't put any address in that slot. Hence the VTABLE will be incomplete. As the VTABLE for Abstract class is incomplete, hence the compiler will not let the creation of object for such class and will display an errror message whenever you try to do so.

Pure Virtual definitions

Pure Virtual functions can be given a small definition in the Abstract class, which you want all the derived classes to have. Still you cannot create object of Abstract class. Also, the Pure Virtual function must be defined outside the class definition. If you will define it inside the class definition, complier will give an error. Inline pure virtual definition is Illegal.

查看更多
倾城 Initia
5楼-- · 2020-02-24 13:23

The error means there are some methods of the class that aren't implemented. You cannot instantiate such a class, so there isn't anything you can do, other than implement all of the methods of the class.

On the other hand, a common pattern is to instantiate a concrete class and assign it to a pointer of an abstrate base class:

class Abstract { /* stuff */ 4};
class Derived : virtual public Abstract { /* implement Abstract's methods */ };

Abstract* pAbs = new Derived; // OK

Just an aside, to avoid memory management issues with the above line, you could consider using a smart pointer, such as an `std::unique_ptr:

std::unique_ptr<Abstract> pAbs(new Derived);
查看更多
狗以群分
6楼-- · 2020-02-24 13:31

In my case i declared a function in COM Control .idl file like

[id(1)] HRESULT MyMethod([in]INT param);

but not declared in my interface .h file like this

STDMETHOD(MyMethod)(INT param);

Problem solved by adding above line into my interface .h file

this might help some one .

查看更多
三岁会撩人
7楼-- · 2020-02-24 13:36

Provide implementation for any pure virtual functions that the class has.

查看更多
登录 后发表回答