c++ initialized specified for non-virtual method

后端 未结 3 1771
轮回少年
轮回少年 2021-02-19 23:26

I have a.h as shown below

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

Then i have b.h as shown below

#include \"a.h\"

class         


        
相关标签:
3条回答
  • 2021-02-19 23:54

    It means that A's do something isn't virtual, but you are trying to make it pure virtual.

    0 讨论(0)
  • 2021-02-19 23:57

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

    Prior to C++11, it was also good practice to repeat virtual in the declaration of the derived class member function, although it's technically not necessary there (as the rule is "once virtual, always virtual").

    C++11 introduced the override keyword which can should used when overriding a virtual member function, to make the code safe against future changes (i.e. if the base function changes signature, the derived code will fail to compile instead of silently becoming wrong). Whether to also include virtual when override is present is up to personal taste/project coding standards. I consider it unnecessary and omit it, but that's just my personal preference.

    0 讨论(0)
  • 2021-02-19 23:58

    The problem is exactly what the compiler says it is.

    class A
    {
    public:
        virtual void doSomething()=0; // virtual keyword needed
    };
    
    0 讨论(0)
提交回复
热议问题