Undefined reference to `vtable for MyClass'



  • Hello, I'm newbie, practicing qt code. I'm trying to call c++ function from qml. But I have a problem, maybe there are few mistakes from code.
    with QT 5.2.0 on Ubuntu

    Let's check;

    I got these issues(3)

    @
    In function 'MyClass::MyClass()':
    undefined reference to `vtable for MyClass'
    home/oak/aeiou/myclass.cpp
    collect2: error: Id returned 1 exit status
    @

    And here's my source codes

    aeiou.pro
    @

    Add more folders to ship with the application, here

    folder_01.source = qml/aeiou
    folder_01.target = qml
    DEPLOYMENTFOLDERS = folder_01

    Additional import path used to resolve QML modules in Creator's code model

    QML_IMPORT_PATH =

    The .cpp file which was generated for your project. Feel free to hack it.

    SOURCES += main.cpp
    myclass.cpp

    Installation path

    target.path =

    Please do not modify the following two lines. Required for deployment.

    include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
    qtcAddDeployment()

    HEADERS +=
    myclass.h
    @

    myclass.h
    @
    #ifndef MYCLASS_H
    #define MYCLASS_H
    #include <QObject>

    class MyClass : public QObject
    {
    Q_OBJECT

    public:
    MyClass();
    void helloMethod(const QString &msg);
    };

    #endif // MYCLASS_H
    @

    main.cpp
    @#include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include <QtDebug>
    #include <QQmlContext>
    #include <myclass.h>

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    
    viewer.rootContext()->setContextProperty("myObject",new MyClass);
    
    viewer.setMainQmlFile&#40;QStringLiteral("qml/aeiou/main.qml"&#41;&#41;;
    viewer.showExpanded();
    return app.exec&#40;&#41;;
    

    }
    @

    myclass.cpp
    @
    #include <myclass.h>
    #include <QDebug>

    MyClass::MyClass(){
    }

    void MyClass::helloMethod(const QString &msg) {
    qDebug() << "c++ method called " << msg;
    }
    @

    main.qml
    @
    import QtQuick 2.0

    Rectangle {
    width: 100; height: 200
    color: "#ad0505"

    MouseArea {
    
        anchors.fill: parent
        onClicked: {
            myObject.helloMethod("hi");
    }
    

    }
    }
    @
    Thanks for helping newbie : )



  • Oh I finally solved the problem

    myclass.h
    @
    #ifndef MYCLASS_H
    #define MYCLASS_H
    #include <QObject>

    class MyClass : public QObject
    {
    Q_OBJECT
    public:
    explicit MyClass(QObject *parent = 0);

    signals:

    public slots:
    void helloMethod(const QString &msg);
    };

    #endif
    @

    myclass.cpp
    @
    #include "myclass.h"
    #include <QDebug>

    MyClass::MyClass(QObject *parent) :
    QObject(parent)
    {
    }
    void MyClass::helloMethod(const QString &msg) {
    qDebug() << "Called the C++ slot with message:" << msg;
    }
    @


Log in to reply
 

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