Samaursa
Samaursa

Reputation: 17271

More information on "cannot instantiate abstract class"

Sometimes I am working with relatively complex (and sometimes confusing - with the way they are laid out by whoever wrote it originally) abstract classes. When inheriting from it, I sometimes encounter cannot instantiate abstract class and most of the time it is because I forgot to declare & implement a pure virtual function. Can I get more information from the compiler about which function it found I did not implement instead of hunting for it?

Upvotes: 3

Views: 2290

Answers (2)

user784668
user784668

Reputation:

Are you using Visual Studio? If so, then switch from Error List tab to Output tab. There will be something like:

main.cpp(8): error C2259: 'foo' : cannot instantiate abstract class
          due to following members:
          'void Foo::method(char)' : is abstract

Upvotes: 5

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361802

Whenever you encounter that message, then it immediately means that you have not defined a pure virtual function in the derived class, and you want to create an instance of it. And if you're using a good compiler then, I'm sure, it indicates which pure virtual function you didn't implement. At least, GCC indicates that.

See the error message here: http://www.ideone.com/83iDk

prog.cpp: In function ‘int main()’:
prog.cpp:11: error: cannot declare variable ‘a’ to be of abstract type ‘A’
prog.cpp:6: note: because the following virtual functions are pure within ‘A’:
prog.cpp:7: note: virtual void A::f()

That is more than enough that you didn't implement A::f().

Upvotes: 2

Related Questions