Direct base ‘QObject’ inaccessible in ‘myClass’ due to ambiguity



  • @// -- header file -------------------------------------------------
    #ifndef MYCLASS_H
    #define MYCLASS_H

    #include <QObject>
    #include <QThread>

    class myClass : public QObject, public QThread
    {
    Q_OBJECT
    public:
    myClass(QObject *parent = 0);
    };

    #endif // MYCLASS_H

    // -- source file ------------------------------------------------
    #include "myclass.h"

    myClass::myClass(QObject *parent) :
    QObject(parent)
    {
    }
    @

    // -- compiler output -----------------------------------------
    myclass.h:9: warning: direct base ‘QObject’ inaccessible in ‘myClass’ due to ambiguity
    myclass.cpp: undefined reference to `vtable for myClass'
    error: collect2: ld returned 1 exit status

    can anyone tell why it fails to compile.???
    and the solution.



  • is it because QThread already inherits QObject class, hence when i derive my class from QObject and QThread, compiler gets confuse to select right constructor for QObject...



  • Hello.

    You can not inherit from two QObjects.

    Well, you should read "this":http://developer.qt.nokia.com/doc/qt-4.8/object.html
    and then "this":http://developer.qt.nokia.com/doc/qt-4.8/metaobjects.html#meta-object-system

    I asked myself the same questions once.
    Object Model and The Meta-Object System are the answers

    best regards



  • [quote author="aekam" date="1328613590"]is it because QThread already inherits QObject class, hence when i derive my class from QObject and QThread, compiler gets confuse to select right constructor for QObject...[/quote]

    "Using the Meta-Object Compiler (moc)":http://developer.qt.nokia.com/doc/qt-4.8/moc.html#moc
    ...
    Multiple Inheritance Requires QObject to Be First
    If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.
    @
    // correct
    class SomeClass : public QObject, public OtherClass
    {
    ...
    };
    @

    Virtual inheritance with QObject is not supported.



  • Inheriting QObject is not necessary, QThread already is one and provides all it's functionality. Simply change it to

    @
    class myClass : public QThread
    {
    Q_OBJECT
    public:

    ...
    @

    [EDIT: code formatting, Volker]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.