Qt class destructor question



  • I seem to be missing something fundamental when trying to use class destructors in Qt (I'm using Qt creator 2.4.0). Hopefully someone in this forum can explain what I'm doing wrong. When I create a new c++ class and try to define a constructor the compiler (or linker, I'm not sure which) often gives me an error. Stripped to the bare minimum of code I created an empty window program with Qt creator with a simple main.cpp of
    @#include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }@

    A mainwindow.h of
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H@

    and a mainwindow.cpp of
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }@

    Pretty simple so far (these are just unaltered Qt creator code) but here is the question: When I create a new class when can I implement a destructor. When I don't make the class a QObject (using Qt creator predefined templates) I have no problem. That is this works:
    @#ifndef MYDTEST_H
    #define MYDTEST_H

    class myDtest
    {
    public:
    myDtest();
    ~myDtest();
    };

    #endif // MYDTEST_H
    @

    Adding the destructor ~myDtest() poses no problem But contrary to published examples the following doesn't work and I don't know why:

    @#ifndef MYDTEST_H
    #define MYDTEST_H

    #include <QObject>

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

    signals:

    public slots:

    };

    #endif // MYDTEST_H
    @

    If I compile and run this without the ~myDtest() destructor it run fine but when I try to compile and run with the destructor I get the following error:
    :-1: error: collect2: ld returned 1 exit status

    Anyone know why?

    The templates for these classes were created by Qt creator.

    Any help will be apopreciated.


  • Moderators

    Are you defining the body of myDtest::~myDtest() in your .cpp file? Just making sure.



  • In that case you should switch to the compiler output pane of Qt Creator. The complete output usually shows what symbole (e.g. not implemented method) is missing. As mlong already stated, it's quite probable that you added the destructor to the class declaration in the header, but you did not implement it in the .cpp file.



  • OOPs, a brain freeze I guess. I should have caught it myself. Thanks for the replies.


Log in to reply
 

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