简体   繁体   English

为非虚方法指定的c ++初始化

[英]c++ initialized specified for non-virtual method

I have ah as shown below 我有啊如下图所示

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

Then i have bh as shown below 然后我有bh如下所示

#include "a.h"

class b: public A
{
public:
    void doSomething();
};

I am just trying to check for syntax errors by trying to compile headers such as g++ -c ah bh 我只是试图通过尝试编译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 means? 这个错误意味着什么?

A member function can only be declared abstract ( = 0 ) if it is virtual. 如果成员函数是虚拟的,则只能将其声明为abstract( = 0 )。 Add the virtual keyword to the function declaration in the base class (in class A ). virtual关键字添加到基类中的函数声明中(在类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之前,在派生类成员函数的声明中重复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 (ie if the base function changes signature, the derived code will fail to compile instead of silently becoming wrong). C ++ 11引入了override关键字,它可以在覆盖虚拟成员函数时使用,以使代码可以安全地防止将来的更改(即,如果基本函数更改签名,派生代码将无法编译而不是默默地变为错误)。 Whether to also include virtual when override is present is up to personal taste/project coding standards. 是否还包括virtual override存在取决于个人品味/项目编码标准。 I consider it unnecessary and omit it, but that's just my personal preference. 我认为这是不必要的,省略了,但这只是我个人的偏好。

The problem is exactly what the compiler says it is. 问题正是编译器所说的。

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

这意味着A做某事不是虚拟的,但你试图让它变成纯粹的虚拟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM