Error: undefined reference to 'vtable for XXX'
-
i have created a base class called Base which inherits from QDialog:
@
class Base : public QDialog
{
Q_OBJECT
public:
Base ();--
};
@
And added this file to PATH: /usr/local/include/ and its source file to /usr/local/src/Now I am using this as a base class in my application:
@
class Test:public Base
{
Q_OBJECT--
};
@The application is giving error:
undefined reference to 'vtable for A'.
Does anyone know why i am getting this error??P.S. :--Here the Base class is not an abstract class.
[edit, code tags added, koahnig]
-
Please check out the forum rules for "code wrappings.":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 This time I have added them for you.
This is a very general error message typically coming up when a class is not properly initialized yet. You need to check out the initialization of members in your class respectively if the sequence of their initialization is correct when doing a recursive initialization. For the last case you should have received a warning as long as the sequence is not correct within the class.
-
One possible (and unfortunately common with Qt and qmake) cause is that qmake did not pick up the Q_OBJECT macro in your class declaration yet. If that is the case, then the Q_OBJECT macro is declaring some methods that are not implemented by moc yet.
Did you re-run qmake on your project before trying to rebuild?
-
Do you have virtual methods?
-
base.h file
@#ifndef BASE_H
#define BASE_H#include <QDialog>
class Base : public QDialog
{
Q_OBJECT
public:protected:
explicit Base(QWidget *parent = 0, Qt::WindowFlags f = 0);signals:
public slots:
};
#endif // BASE_H@
base.cpp file
@#include "base.h"Base::Base(QWidget *parent, Qt::WindowFlags f):
QDialog(parent, f)
{
}@Added base.h in PATH: /usr/local/include/ and base.cpp in PATH:/usr/local/src/.., since i dont want to add this base class in my application explicitely.
TestBase project:
This is the class inherits Base class:
test.h file
@#ifndef TEST_H
#define TEST_H#include "base.h"
class Test : public Base
{
Q_OBJECTpublic:
explicit Test(QWidget *parent = 0);
~Test();private:
};
#endif // TEST_H
@test.cpp file":
@#include "test.h"Test::Test(QWidget *parent) :
Base(parent, Qt::FramelessWindowHint ),
{
}Test::~Test()
{
}
@main.cpp:
@#include <QtGui/QApplication>
#include "dialog.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();return a.exec();
}
@Errors after make command:
test.o: In functionTest::~Test()': test.cpp:(.text+0x1f): undefined reference to
vtable for Base'
test.cpp:(.text+0x27): undefined reference tovtable for Base' test.o: In function
Test::Test(QWidget*)':
test.cpp:(.text+0x96): undefined reference toBase::Base(QWidget*, QFlags<Qt::WindowType>)' test.cpp:(.text+0xee): undefined reference to
vtable for Base'
test.cpp:(.text+0xf6): undefined reference tovtable for Base' moc_test.o: In function
Test::qt_metacall(QMetaObject::Call, int, void**)':
moc_test.cpp:(.text+0x31): undefined reference toBase::qt_metacall(QMetaObject::Call, int, void**)' moc_test.o: In function
Test::qt_metacast(char const*)':
moc_test.cpp:(.text+0x77): undefined reference toBase::qt_metacast(char const*)' moc_test.o:(.rodata+0x20): undefined reference to
Base::staticMetaObject'
moc_test.o:(.rodata._ZTI4Test[typeinfo for Test]+0x10): undefined reference to `typeinfo for Base'
collect2: ld returned 1 exit status
make: *** [testMMIBase] Error 1 -
[quote author="Kritika" date="1391494986"]Added base.h in PATH: /usr/local/include/ and base.cpp in PATH:/usr/local/src/.., since i dont want to add this base class in my application explicitely.
[/quote]
That is interesting. What do you mean by that? Do you mean that you're not actually compiling in base.h and base.cpp into your application? If so: that won't work. If you don't want Base in your main application, you'll need to put it in a library that you link against. Otherwise, how is your application to know at run time what base actually is? -
If you don't include base.h and base.cpp in your project directory, then they will not be compiled.
You have two options:
Put base.h and base.cpp in your project directory.
Compile base.h and base.cpp into an an external library. Then, link that library to your project.
-
@JKSH- Thanks....
Like u said, i compiled this base class, this created four versioned libraries:
libbase.so
libbase.so.1
libbase.so.1.0
libbase.so.1.0.0
and then, i linked the TestBase project against it. The application did build successfully, but giving another error on run:"error while loading shared libraries: libbase.so.1: cannot open shared object file: no such file or directory"
Can anybody explain why i am getting this error..
-
The problem is fixed..
Now, i am adding library in the application by the option Add Library on right click. This is linking library as well as including header file. And this is working well...Although i still couldnt figure out why previous thing was not working. -
[quote author="Kritika" date="1391529236"]The problem is fixed..[/quote]Great! :)
[quote]Although i still couldnt figure out why previous thing was not working.[/quote]Because you didn't tell your program where to find the library previously.