Strange errors with my code



  • Hi guys,

    Please look at my code.
    This is Hello.h:

    #ifndef HELLO_H
    #define HELLO_H
    
    #include <QDialog>
    
    class QCheckBox;
    class QLabel;
    class QLineEdit;
    class QPushButton;
    
    class Hello : public QDialog
    {
        Q_OBJECT
    public:
        Hello(QWidget* parent = 0);
    
    signals:
        void findNext(const QString& str, Qt::CaseSensitivity cs);
        void findPrevious(const QString& str, Qt::CaseSensitivity cs);
    
    private slots:
        void findClicked();
        void enableFindButton(const QString& text);
    
    private:
        QLabel* label;
        QLineEdit* lineEdit;
        QCheckBox* caseCheckBox;
        QCheckBox* backwardCheckBox;
        QPushButton* findButton;
        QPushButton* closeButton;
    };
    
    #endif // HELLO_H
    

    And this one, Hello.cpp

    #include <QtWidgets>
    #include "hello.h"
    
    Hello::Hello(QWidget *parent)
        : QDialog(parent)
    {
        label = new QLabel(tr("Find &What"));
        lineEdit = new QLineEdit;
        label -> setBuddy(lineEdit);
    
        caseCheckBox = new QCheckBox (tr("Match &case"));
        backwardCheckBox = new QCheckBox (tr("Search &backward"));
    
        findButton = new QPushButton (tr("&Find"));
        findButton -> setDefault(true);
        findButton -> setEnabled(false);
        closeButton = new QPushButton(tr("Close"));
    
        connect(lineEdit, SIGNAL(textChanged(const QString&)),
                this, SLOT(enableFindButton(const QString&)));
        connect(findButton, SIGNAL(clicked()),
                this, SLOT(findClicked()));
        connect(closeButton, SIGNAL(clicked()),
                this, SLOT(close()));
    
        QHBoxLayout* topLeftLayout = new QHBoxLayout;
        topLeftLayout -> addWidget(label);
        topLeftLayout -> addWidget(lineEdit);
    
        QVBoxLayout* leftLayout = new QVBoxLayout;
        leftLayout -> addLayout(topLeftLayout);
        leftLayout -> addWidget(caseCheckBox);
        leftLayout -> addWidget(backwardCheckBox);
    
        QVBoxLayout* rightLayout = new QVBoxLayout;
        rightLayout -> addWidget(findButton);
        rightLayout -> addWidget(closeButton);
        rightLayout -> addStretch();
    
        QHBoxLayout* mainLayout = new QHBoxLayout;
        mainLayout -> addLayout(leftLayout);
        mainLayout -> addLayout(rightLayout);
        setLayout(mainLayout);
    
        setWindowTitle((tr("Find")));
        setFixedHeight(sizeHint().height());
    }
    
    //-------------------------------------------------------
    
    void Hello::findClicked()
    {
        QString text = lineEdit -> text();
        Qt::CaseSensitivity cs = caseCheckBox -> isChecked() ? Qt::CaseSensitive
                                                             : Qt::CaseInsensitive;
        if(backwardCheckBox -> isChecked()) {
            emit findPrevious(text, cs);
        } else {
            emit findNext(text, cs);
        }
    }
    
    //-------------------------------------------------
    
    void Hello::enableFindButton(const QString& text)
    {
        findButton -> setEnabled(!text.isEmpty());
    }
    

    And this, main.cpp:

    #include <QApplication>
    #include <hello.h>
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
        Hello* dialog = new Hello;
        dialog -> show();
    
        return app.exec();
    }
    

    I get some strange errors that I don't know the reason for them, like:
    C:\Users...\Qt\Hello\hello.cpp:5: error: undefined reference to `vtable for Hello'.

    This is for line 5 of Hello.cpp ( : QDialog(parent))

    Would you please help me?


  • Moderators

    You try to assign a QWidget pointer to QDialog.

    Change to

    class Hello : public QDialog
    {
        Q_OBJECT
    public:
        Hello(QDialog* parent = 0);
    

    and

    Hello::Hello(QDialog *parent)
        : QDialog(parent)
    


  • @koahnig :
    Thanks for the answer. I understand your reason but do you have the book C++-GUI-Programming-with-Qt-4-2ndEdition on your hand to have a look at page 29 of it?
    There it mentions these:

    class Hello : public QDialog
    {
        Q_OBJECT
    public:
        Hello(QWidget* parent = 0);
    

    and

    Hello::Hello(QWidget *parent)
        : QDialog(parent)
    {
    

    I also modified my code as you suggested but get the errors yet! :(


  • Qt Champions 2016

    Hi

    • "undefined reference to `vtable "

    From build menu, try
    Clean All
    qmake
    Build All



  • @mrjj said in Strange errors with my code:

    Hi

    • "undefined reference to `vtable "

    From build menu, try
    Clean All
    qmake
    Build All

    Thank you. You solved the issues.

    Could I ask why I faced this kind of problem? What was the reason?


  • Moderators

    Sometimes a fresh rebuild simply helps.

    Especially when you have a lot of different source and header not all dependencies are always covered and the system cannot detect.
    This is just a trial to explain, it is very often a mystery to me why you are ending up in such failures.

    The sequence given by @mrjj is basically some sort of Ctrl+Alt+Del for Qt creator


  • Qt Champions 2016

    @tomy said in Strange errors with my code:

    What was the reason?

    Stale translation units (object files). Probably Creator missed that you changed something, just as @koahnig explained, and didn't recompile the .cpp, which in turn led to the linker error.


  • Lifetime Qt Champion

    Hi,

    To add to my fellows, the vtable error usually comes when adding/removing the Q_OBJECT macro. This one requires to re-run qmake to refresh the build setup with the add/removed information.



  • So whenever I will see such error (vtable) again, I will do the steps mrjj said. :)
    Thanks so much to all, especially to SGaist for his simple language.


Log in to reply
 

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