earth
earth

Reputation: 955

Simple app getting "undefined reference to vtable" error? [Qt]

I am trying to make the code found on this page compile. All I want to do is replicate what that page describes. I keep getting an error that says:

main.cpp:13: error: undefined reference to `vtable for myMainWindow'

Here's my code, which is pretty much exactly like the code on that page:

main.cpp

#include <QApplication>
#include <QDialog>
#include <QWidget>
#include <QGridLayout>
#include <QPushButton>
#include <QMainWindow>
#include <QBitmap>

class myMainWindow : public QMainWindow
{

public:
myMainWindow():QMainWindow()
  {
    setMask((new QPixmap("saturn.png"))->mask());
      QPalette* palette = new QPalette();
      palette->setBrush(QPalette::Background,QBrush(QPixmap("saturn.png")));
      setPalette(*palette);

      setWindowFlags(Qt::FramelessWindowHint);
      QWidget *centralWidget = new QWidget(this);
      QGridLayout *layout = new QGridLayout();

      centralWidget->setLayout(layout);

      QPushButton* button1 = new QPushButton("Button 1");
      button1->setFixedSize(80,50);

      layout->addWidget(button1,0,0);

      setCentralWidget(centralWidget);
  };
  ~myMainWindow();
};

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  myMainWindow *window = new myMainWindow();

    window->resize(600, 316);
    window->show();
    return app.exec();
}

I read up on why this might be occurring and saw that it was something about needing to have the class defined in a header file. How would I go about doing that correctly given this code?

Upvotes: 0

Views: 352

Answers (2)

dtech
dtech

Reputation: 49309

Aside from missing the destructor definition, you are also missing the Q_OBJECT macro, which is mandatory for all QObject derived classes. If you had that, you'd get another error about the MOC generated files, when you define QObject derived classes in main.cpp you need to include the MOC file manually. This is not the case if you use a dedicated h and cpp file for QObject derived classes.

Upvotes: 2

Anedar
Anedar

Reputation: 4275

As stated in the comments: The missing function body for ~myMainWindow() was the problem.

Upvotes: 1

Related Questions