PDA

View Full Version : Undifined Refrence: vtable



mweber1488
4th January 2011, 04:10
I just started learning Qt and I have been working through the example on this page: http://doc.qt.nokia.com/4.7/gettingstartedqt.html
The problem is that when I get to sub classing QWidget I start getting the following linker errors:

notepad.cpp|3|undefined reference to `vtable for Notepad'|
obj\Debug\notepad.o:C:\programming\c\test\notepad. h|10|undefined reference to `Notepad::staticMetaObject'|

The code is exactly the same as the examples, I am using Qt 4.7.1 and codeblocks as the ide.

tbscope
4th January 2011, 04:17
Clean you project (that is, remove any generated files), run qmake again, rebuild

nroberts
4th January 2011, 17:14
I just started learning Qt and I have been working through the example on this page: http://doc.qt.nokia.com/4.7/gettingstartedqt.html
The problem is that when I get to sub classing QWidget I start getting the following linker errors:

notepad.cpp|3|undefined reference to `vtable for Notepad'|
obj\Debug\notepad.o:C:\programming\c\test\notepad. h|10|undefined reference to `Notepad::staticMetaObject'|

The code is exactly the same as the examples, I am using Qt 4.7.1 and codeblocks as the ide.

The moc output for notepad isn't getting linked in.

squidge
4th January 2011, 17:38
The moc output for notepad isn't getting linked in.

and the solution to fix that is in post #2

Zlatomir
4th January 2011, 17:41
When you sub-class don't forget the Q_OBJECT macro:


class Notepad : public QWidget {
Q_OBJECT //this one is important if you use signals/slots or other qmetaobject goodies
//.... rest of the class definition


Also for each class use a header .h file for declaration and a .cpp file for definitions.

If you write classes with Q_OBJECT in the main.cpp file (a and have only that one .cpp file) you should use:

#include "main.moc"
at the end of the .cpp file
Replace main.cpp with YourFileName.cpp

mweber1488
5th January 2011, 00:52
and the solution to fix that is in post #2Actually its not as
codeblocks as the ide and doesn't use qmake.
So I figure out the moc thing just before I looked here as I tried building under QtCreator but now I am getting the following error (in both QtCreator and codeblocks):

debug/moc_notepad.o:C:\programming\c\test\test-build-desktop/debug/moc_notepad.cpp:74: undefined reference to `Notepad::open()'

debug/moc_notepad.o:C:\programming\c\test\test-build-desktop/debug/moc_notepad.cpp:75: undefined reference to `Notepad::save()'

debug/moc_notepad.o:C:\programming\c\test\test-build-desktop/debug/moc_notepad.cpp:76: undefined reference to `Notepad::quit()'

For completeness here is the entire code:
notepad.h

#ifndef _NOTEPAD_H_
#define _NOTEPAD_H_

#include <QtGui>

class Notepad: public QMainWindow
{
Q_OBJECT

public:
Notepad();

private slots:
void open();
void save();
void quit();

private:
QTextEdit *textEdit; // The text editor

QAction *openAction; // For the widgets that perform open
QAction *saveAction; // Save shit
QAction *quitAction; // Quit shit

QMenu *fileMenu; // Holds the menu options
};

#endif

notepad.cpp

#include "notepad.h"

Notepad::Notepad()
{
// Do the actions first
openAction = new QAction(tr("&Open"), this);
saveAction = new QAction(tr("&Sace"), this);
quitAction = new QAction(tr("E&xit"), this);

// Now connect the signals to the actions
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

// Now set the menu bar
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addSeparator();
fileMenu->addAction(quitAction);

textEdit = new QTextEdit;
setCentralWidget(textEdit);

setWindowTitle(tr("Notepad"));
}

main.cpp

#include "notepad.h"

int main(int argc, char ** argv)
{
QApplication app(argc, argv);

Notepad notepad;
notepad.show();

return app.exec();
}

norobro
5th January 2011, 02:35
The error messages you are getting now tell you what is wrong:
undefined reference to `Notepad:: open()'
You've declared your slots but you haven't defined them.

mweber1488
5th January 2011, 03:20
Ahh, the example I have been following failed to mention that and I have been using D (http://www.digitalmars.com\d) for so long that I am not used to classes being in stops. Thanks for all the help folks.

tbscope
5th January 2011, 04:25
As always:
1. Learn how your tools work
2. Provide all your information if you want help. My crystal ball often doesn't see if you use qmake or not.

squidge
5th January 2011, 16:18
It is always recommended that if your just getting started with Qt, you should make life at least a little easier for yourself by using the Qt SDK (ie, Qt Creator) and THEN moving to your preferred IDE when you understand how everything works rather than trying to jump in with a new framework from the start.

nroberts
5th January 2011, 17:28
I am not used to classes being in stops.

What be this?