38

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); 
10
  • @dasblinkenlight IIRC, that is optional. The latter default constructs the object, however. But not an error. See also: ideone.com/LWbg6
    – Drise
    Commented Aug 6, 2012 at 18:41
  • 1
    @dasblinkenlight Hmm, no, the parentheses are not needed. Commented Aug 6, 2012 at 18:41
  • 5
    @dasblinkenlight Did too much Java and C#, didn't you? Commented Aug 6, 2012 at 18:42
  • 8
    btw. why so many downvotes? I find the question pretty clear and structured. It is a beginner topic sure, but the question is okay imho
    – Fiktik
    Commented Aug 6, 2012 at 18:46
  • 6
    I wish I could downvote all of these elitist flamerboys.
    – Alexandru
    Commented Jan 24, 2014 at 20:34

9 Answers 9

49

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 abstract 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 std::unique_ptr:

std::unique_ptr<Abstract> pAbs(new Derived);
5
  • Well, I tried commenting out all the methods in AmbientOccluder and did a build to make sure all the methods were accounted for. They are. So on this level, it is not because some of the medthods do not have bodies. I will try it on the parent class.
    – xarzu
    Commented Aug 6, 2012 at 18:49
  • @xarzu If AmbientOcluder has a base class, look for unimplemented methods there too. Otherwise, your compiler error is most misleading! Commented Aug 6, 2012 at 18:52
  • @juanchopanza AmbientOcluder inherits from Light. Commented Aug 6, 2012 at 18:55
  • 7
    To see which methods haven't been implemented, go to View -> Output. Commented May 3, 2018 at 13:38
  • In C++ is it possible to provide an anonymous implementation at the moment of instantiating an object of "AbstractClass" type? Just like in Java
    – M.Ionut
    Commented Jun 19, 2022 at 20:51
43

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'
2
  • 3
    Damn useful tip to know WHICH methods aren't implemented.
    – Bob Stein
    Commented Apr 9, 2019 at 14:01
  • 6
    This should be the top answer. The output pane tells you which class/method hasn't been implemented. Commented Sep 11, 2019 at 11:52
11

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.

4

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

1

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.

0

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 .

0

If anyone is getting this error from a function, try using a reference to the abstract class in the parameters instead.

void something(Abstract bruh){
}

to

void something(Abstract& bruh){
}

Can't construct an abstract class, even from a subclass. Abstract classes are basically templates for other classes to be built on and don't have constructors themselves. It's kind of another way to do an interface that involves inheritance.

There's a lot of stuff online about polymorphism, and it's all dumb. C++ makes it so that you can inherit from multiple classes at the same time, so abstract classes and interfaces are the same there. I think the reason why abstract classes exist is because some languages, like Java, can only inherit/extend from one class only.

Abstract classes are quite literally an abstract concept. They are parents of classes, but only in our minds. They are an answer to a question that doesn't have a right answer. Like, "What is the predecessor to GTA?" and you'd say, "Well, there's Race n' Chase" but that's not even a correct answer because they're the same game, just with the name changed. Abstract classes are the answer to "what's the parent of that class"? Like you could say "Oh, a String is just a character array" and "a character array is just a couple iterators" and "iterators are just pointers". Abstract classes allow us to formalize these stupid answers into inheritance.

1
  • Can you explain why this works?
    – siride
    Commented Jan 26, 2022 at 2:06
0

Also, make sure if your abstract class is pure abstract (meaning all the methods are const = 0), to check their implementations have a const override.

class Abstract {
public:
    virtual void method() const = 0;
}

class Derived : public Abstract {
public:
    void method() const override {
    }
}
-1

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

See if it helps for some one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.