Undifined Refrence: vtable
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.
Re: Undifined Refrence: vtable
Clean you project (that is, remove any generated files), run qmake again, rebuild
Re: Undifined Refrence: vtable
Quote:
Originally Posted by
mweber1488
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.
Re: Undifined Refrence: vtable
Quote:
Originally Posted by
nroberts
The moc output for notepad isn't getting linked in.
and the solution to fix that is in post #2
Re: Undifined Refrence: vtable
When you sub-class don't forget the Q_OBJECT macro:
Code:
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:
Code:
#include "main.moc"
at the end of the .cpp file
Replace main.cpp with YourFileName.cpp
Re: Undifined Refrence: vtable
Quote:
Originally Posted by
squidge
and the solution to fix that is in post #2
Actually its not as
Quote:
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):
Code:
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
Code:
#ifndef _NOTEPAD_H_
#define _NOTEPAD_H_
#include <QtGui>
{
Q_OBJECT
public:
Notepad();
private slots:
void open();
void save();
void quit();
private:
QAction *openAction;
// For the widgets that perform open
QMenu *fileMenu;
// Holds the menu options };
#endif
notepad.cpp
Code:
#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);
setCentralWidget(textEdit);
setWindowTitle(tr("Notepad"));
}
main.cpp
Code:
#include "notepad.h"
int main(int argc, char ** argv)
{
Notepad notepad;
notepad.show();
return app.exec();
}
Re: Undifined Refrence: vtable
The error messages you are getting now tell you what is wrong:
Quote:
undefined reference to `Notepad:: open()'
You've declared your slots but you haven't defined them.
Re: Undifined Refrence: vtable
Ahh, the example I have been following failed to mention that and I have been using D for so long that I am not used to classes being in stops. Thanks for all the help folks.
Re: Undifined Refrence: vtable
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.
Re: Undifined Refrence: vtable
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.
Re: Undifined Refrence: vtable
Quote:
Originally Posted by
mweber1488
I am not used to classes being in stops.
What be this?