欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

cannot convert from pointer to base class 'QObject' to pointer to derived class 'subClass' via virtual base 'baseClass'

程序员文章站 2022-03-12 16:14:02
cannot convert from pointer to base class 'QObject' to pointer to derived class 'subClass' via virtual base 'baseClass' ......

qt 编译不过的另一个问题:

1. 新建一个console工程

qt -= gui

config += c++11 console
config -= app_bundle

# the following define makes your compiler emit warnings if you use
# any feature of qt which as been marked deprecated (the exact warnings
# depend on your compiler). please consult the documentation of the
# deprecated api in order to know how to port your code away from it.
defines += qt_deprecated_warnings

# you can also make your code fail to compile if you use deprecated apis.
# in order to do so, uncomment the following line.
# you can also select to disable deprecated apis only up to a certain version of qt.
#defines += qt_disable_deprecated_before=0x060000    # disables all the apis deprecated before qt 6.0.0

sources += \
        main.cpp \
    baseclass.cpp \
    subclass.cpp \
    subclass2.cpp
    #grandclass.cpp

# default rules for deployment.
qnx: target.path = /tmp/$${target}/bin
else: unix:!android: target.path = /opt/$${target}/bin
!isempty(target.path): installs += target

headers += \
    baseclass.h \
    subclass.h \
    subclass2.h
    #grandclass.h

2. 添加baseclass

baseclass.h

#ifndef baseclass_h
#define baseclass_h

#include "qobject"

class baseclass: public qobject
{
    q_object

public:
    baseclass(qobject * p = 0);
    virtual ~baseclass() {}

    virtual void vmethod(int) = 0;

signals:
    void signal1();


};

#endif // baseclass_h

baseclass.cpp

#include "baseclass.h"

baseclass::baseclass(qobject * p) : qobject(p)
{

}

3. 添加subclass

subclass.h

#ifndef subclass_h
#define subclass_h

#include "baseclass.h"

class subclass: virtual public baseclass
{
    q_object

public:
    subclass(qobject * p = 0);
    virtual ~subclass() {};
    virtual void vmethod(int) = 0;

signals:
    void signal2();

};

#endif // subclass_h

subclass.cpp

#include "subclass.h"

subclass::subclass(qobject * p) : baseclass (p)
{

}

4. 添加subclass2

subclass2.h

#ifndef subclass2_h
#define subclass2_h

#include "baseclass.h"

class subclass2 : virtual public baseclass
{
    q_object

public:
    subclass2(qobject * p = 0);

    virtual void vmethod(int) = 0;

};

#endif // subclass2_h

subclass2.cpp

#include "subclass2.h"

subclass2::subclass2(qobject * p) : baseclass (p)
{

}

5 main.cpp

#include <qcoreapplication>
#include "subclass.h"
//#include "grandclass.h"

int main(int argc, char *argv[])
{
    qcoreapplication a(argc, argv);


    //grandclass * sub = new grandclass();

    return a.exec();
}

 

编译报错:/build-testqtsignalbug-qt5_9_4_zynq-debug/moc_subclass.cpp:68: error: cannot convert from pointer to base class 'qobject' to pointer to derived class 'subclass' via virtual base 'baseclass'

subclass *_t = static_cast<subclass *>(_o);
^

 cannot convert from pointer to base class 'QObject' to pointer to derived class 'subClass' via virtual base 'baseClass'

 

解决方法: 将subclass里的signals 删掉, 即不在subclass里声明信号量。

原因:不详。