C ++ initialized for non-virtual method

I have ah as below

class A { public: void doSomething()=0; }; 

Then I have bh as below

 #include "ah" class b: public A { public: void doSomething(); }; 

I'm just trying to check for syntax errors by trying to compile headers like g++ -c ah bh

and i get below errror

ah:4: error: initializer specified for non-virtual method 'void A::doSomething()'

What does this error mean?

+7
source share
3 answers

A member function can be declared abstract ( = 0 ) only if it is virtual. Add the virtual to the function declaration in the base class (in class A ).

Before C ++ 11, it was also good practice to repeat virtual in declaring a derived member function of a class, although this is not technically necessary there (since the rule is "once virtual, always virtual").

In C ++ 11, the override keyword is introduced, which can be used when redefining a virtual member function to protect the code from future changes (i.e., if the base function changes its signature, the derived code cannot be compiled instead of becoming silently silent). Enabling or disabling virtual if there is an override depends on the personal taste / coding standards of the project. I consider this unnecessary and omit it, but this is only my personal preference.

+10
source

The problem is what the compiler says.

 class A { public: virtual void doSomething()=0; // virtual keyword needed }; 
+4
source

This means that A makes something non-virtual, but you are trying to make it pure virtual.

0
source

All Articles