Results 1 to 5 of 5

Thread: undefined reference to vtable error

  1. #1
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default undefined reference to vtable error

    Here is a simple Qt code I have written to study signals and slots concept :-
    Qt Code:
    1. #include<QObject>
    2. class A : public QObject{
    3. Q_OBJECT
    4. public:
    5. A(){val=0;}
    6. public slots:
    7. void changeval(int n){
    8. val=n;
    9. emit changed(val);
    10. }
    11. signals:
    12. void changed(int n);
    13. private:
    14. int val;
    15. };
    16. int main()
    17. {
    18. A l,m;
    19. }
    To copy to clipboard, switch view to plain text mode 

    I followed the following to "make" it
    1.qmake -project
    2.qmake
    3.make
    And at the last phase, I got an error as :
    Qt Code:
    1. communicate.o: In function `main':
    2. communicate.cpp:(.text+0x2d): undefined reference to `vtable for A'
    3. communicate.cpp:(.text+0x4d): undefined reference to `vtable for A'
    4. communicate.cpp:(.text+0x68): undefined reference to `vtable for A'
    5. communicate.cpp:(.text+0x8d): undefined reference to `vtable for A'
    6. collect2: ld returned 1 exit status
    7. make: *** [firstapp] Error 1
    To copy to clipboard, switch view to plain text mode 
    I don't have no idea how does this happen. Can you please help me out?
    Last edited by PaulDaviesC; 20th April 2012 at 14:15.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference to vtable error

    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.

  3. The following user says thank you to Lesiok for this useful post:

    PaulDaviesC (20th April 2012)

  4. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: undefined reference to vtable error

    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.

  5. The following user says thank you to Zlatomir for this useful post:

    PaulDaviesC (20th April 2012)

  6. #4
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: undefined reference to vtable error

    Finally I fixed it by keeping a separate header file.

  7. #5
    Join Date
    Apr 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: undefined reference to vtable error

    I had the same problem yesterday.

    See the thread http://www.qtcentre.org/threads/4857...Y-and-Q_OBJECT

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

Similar Threads

  1. Undefined reference to vtable.
    By hakermania in forum Newbie
    Replies: 3
    Last Post: 10th September 2011, 18:10
  2. [SOLVED] Qt: Signals and slots Error: undefined reference to `vtable for
    By TheIndependentAquarius in forum Qt Programming
    Replies: 4
    Last Post: 2nd May 2011, 10:51
  3. Replies: 5
    Last Post: 7th November 2007, 14:46
  4. Error : undefined reference to `vtable for MyClass'
    By joseph in forum Qt Programming
    Replies: 23
    Last Post: 15th June 2007, 11:21
  5. Replies: 5
    Last Post: 14th February 2006, 23:43

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.