How do you handle a cannot instantiate abstract class error in C++

How do you handle a cannot instantiate abstract class error in C++?

How do you handle a cannot instantiate abstract class error in C++?

The error means there are some methods of the class that arent implemented. You cannot instantiate such a class, so there isnt 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 Abstracts 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);

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

c:pathtoyourcode.cpp(42): error C2259: AmbientOccluder : cannot instantiate abstract class
          due to following members:
          ULONG MysteryUnimplementedMethod(void) : is abstract
          c:pathtosomeinclude.h(8) : see declaration of MysteryUnimplementedMethod

How do you handle a cannot instantiate abstract class error in C++?

An abstract class cannot be instantiated by definition. In order to use this class, you must create a concrete subclass which implements all virtual functions of the class. In this case, you most likely have not implemented all the virtual functions declared in Light. This means that AmbientOccluder defaults to an abstract class. For us to further help you, you should include the details of the Light class.

Related posts on C++  :

Leave a Reply

Your email address will not be published.