Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt class destructor question
Forum Updated to NodeBB v4.3 + New Features

Qt class destructor question

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 11.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pt_develop
    wrote on 29 Feb 2012, 20:09 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on 29 Feb 2012, 20:26 last edited by
      #2

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

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on 29 Feb 2012, 20:47 last edited by
        #3

        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.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Pt_develop
          wrote on 1 Mar 2012, 17:02 last edited by
          #4

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

          1 Reply Last reply
          0

          1/4

          29 Feb 2012, 20:09

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved