zoukankan      html  css  js  c++  java
  • 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);
    ^

     

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

    原因:不详。

  • 相关阅读:
    Android学习笔记----天地图API开发之UnsatisfiedLinkError
    Android学习笔记----ArcGIS在线地图服务(Android API)坐标纠偏
    Android学习笔记----Java字符串MD5加密
    Android学习笔记----Java中的字符串比较
    MySQL数据库导入错误:ERROR 1064 (42000) 和 ERROR at line xx: Unknown command ''.
    新浪微博POI点签到数据及可视化的初步成果
    Chrome调试本地文件无法使用window.opener对象进行窗口间值传递
    132、Android安全机制(2) Android Permission权限控制机制(转载)
    用addOnGlobalLayoutListener获取View的宽高
    Android Studio解决unspecified on project app resolves to an APK archive which is not supported
  • 原文地址:https://www.cnblogs.com/crazyghostvon/p/moc_signals_failed.html
Copyright © 2011-2022 走看看