PDA

View Full Version : undefined reference to vtable error



PaulDaviesC
20th April 2012, 13:53
Here is a simple Qt code I have written to study signals and slots concept :-

#include<QObject>
class A : public QObject{
Q_OBJECT
public:
A(){val=0;}
public slots:
void changeval(int n){
val=n;
emit changed(val);
}
signals:
void changed(int n);
private:
int val;
};
int main()
{
A l,m;
}

I followed the following to "make" it
1.qmake -project
2.qmake
3.make
And at the last phase, I got an error as :


communicate.o: In function `main':
communicate.cpp:(.text+0x2d): undefined reference to `vtable for A'
communicate.cpp:(.text+0x4d): undefined reference to `vtable for A'
communicate.cpp:(.text+0x68): undefined reference to `vtable for A'
communicate.cpp:(.text+0x8d): undefined reference to `vtable for A'
collect2: ld returned 1 exit status
make: *** [firstapp] Error 1

I don't have no idea how does this happen. Can you please help me out?

Lesiok
20th April 2012, 14:29
Q_OBJECT macro is correctly supported only in header files. You must create h file with definition of class A and cpp file with its implementation.

Zlatomir
20th April 2012, 14:30
The simple ("long term") solution is to define classes in their own .h and .cpp files.
Else you will need to include "main.moc" (it might have a different name) so that the compiler can "see" the generated code for your signals and slots, and that code is the .moc file.

PaulDaviesC
20th April 2012, 14:36
Finally I fixed it by keeping a separate header file.

zephod
20th April 2012, 16:11
I had the same problem yesterday.

See the thread http://www.qtcentre.org/threads/48574-Questions-about-Q_PROPERTY-and-Q_OBJECT

Perhaps the docs could changed to make this clearer for us newbies?