C++:错误:不允许使用间接非虚拟基类

C++: Error: Indirect nonvirtual base class is not allowed

本文关键字:基类 虚拟 错误 不允许 C++      更新时间:2023-10-16

我正在尝试用c ++和OpenGL创建某种基本的UI。有了这个,我正在使用一个名为OGLRectangle的类:

class OGLRectangle : public Renderable, public Listener
{
public:
                    OGLRectangle();
                    OGLRectangle(float cX, float cY);
                    ~OGLRectangle();
...
}

它由包含所有按钮类型之间的共享方法的 Button 类继承:

 class Button : public OGLRectangle

最后,ButtonBrowse 的类继承自此并包含文件打开的方法:

class ButtonBrowse : public Button
{
    public:
        ButtonBrowse(float cX, float cY);
...
}

现在我说得对,要在 ButtonBrowse 中传递构造函数的参数,我需要在构造函数中做这样的事情:

ButtonBrowse::ButtonBrowse(float cX, float cY) : OGLRectangle(cX, cY)
{
...
}

如果是这样,为什么我会得到标题中的间接非虚拟错误?

你需要调用 Button 的构造函数,然后调用OGLRectangle构造函数。

ButtonBrowse::ButtonBrowse(float cX, float cY) : Button(cX, cY)
{
...
}

只要Button设置了构造函数以将参数传递到其直接基类OGLRectangle,您应该没问题。

编译器不允许直接传递,您应该逐步将这些值传递给基类的构造函数目标。