Results 1 to 19 of 19

Thread: Qt Undefined Reference to vtable

  1. #1
    Join Date
    Jan 2010
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt Undefined Reference to vtable

    Hey guys,

    I have no clue why I'm getting this error: Undefined Reference to vtable

    Here's my code:
    Qt Code:
    1. #ifndef JAUS_H
    2. #define JAUS_H
    3.  
    4. #include <QtNetwork>
    5.  
    6. struct Jaus_header
    7. {
    8. unsigned short message_properties;
    9. unsigned short command_code;
    10. QByteArray dest_id[4];
    11. QByteArray source_id[4];
    12. unsigned short data_control;
    13. unsigned short sequence_num;
    14. };
    15.  
    16. class Jaus_neighbor
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. void handshake();
    22. void capabilities_discover();
    23. void sys_management();
    24. void close_conn();
    25.  
    26. private:
    27. Jaus_header head;
    28.  
    29. unsigned short command;
    30. QByteArray dest[4];
    31. QByteArray source[4];
    32. unsigned short control;
    33. unsigned short seq_num;
    34.  
    35. QUdpSocket *udpSocket;
    36.  
    37. private slots:
    38. void read_jheader();
    39. void send_jheader();
    40. void set_data();
    41.  
    42. void do_command();
    43. void startBroadcasting();
    44. void stopBroadcasting();
    45. void sendDatagram();
    46. //void verifyDatagram();
    47. };
    48.  
    49. #endif // JAUS_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "jaus.h"
    2.  
    3. void Jaus_neighbor::startBroadcasting()
    4. {
    5.  
    6. }
    7.  
    8. void Jaus_neighbor::stopBroadcasting()
    9. {
    10.  
    11. }
    12.  
    13. void Jaus_neighbor::sendDatagram()
    14. {
    15. //udpSocket->writeDatagram(message, QHostAddress::LocalHost, 9000);
    16. }
    17.  
    18.  
    19. void Jaus_neighbor::handshake()
    20. {
    21. Jaus_neighbor::startBroadcasting();
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <iostream>
    3. #include "jaus.h"
    4. using namespace std;
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10. cout << "Initializing Jaus Connections..." << flush;
    11. Jaus_neighbor cop;
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Please help!

  2. #2
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Qt Undefined Reference to vtable

    What happens if you include QObject and make Jaus_neighbor a subclass of QObject?
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  3. #3
    Join Date
    Jan 2010
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Undefined Reference to vtable

    in addition, i tried adding
    Qt Code:
    1. udpSocket = new QUdpSocket(this);
    To copy to clipboard, switch view to plain text mode 
    to the Jaus_neighbor constructor and keep getting the following error:

    error: no matching function for call to 'QUdpSocket::QUdpSocket(Jaus_neighbor* const)

  4. #4
    Join Date
    Jan 2010
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Undefined Reference to vtable

    Quote Originally Posted by Archimedes View Post
    What happens if you include QObject and make Jaus_neighbor a subclass of QObject?
    Now I get an error saying multiple constructors defined:
    multiple definition of `Jaus_neighbor::Jaus_neighbor()

    Qt Code:
    1. Jaus_neighbor::Jaus_neighbor()
    2. {
    3. udpSocket = new QUdpSocket(this);
    4. for (int i = 0; i < 4; i++)
    5. {
    6. head.dest_id[i] = 0;
    7. head.source_id[i] = 0;
    8. }
    9. head.sequence_num = 0;
    10. head.data_control = 0;
    11. head.message_properties = 0;
    12. head.command_code = NULL;
    13. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2010
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Undefined Reference to vtable

    Nevermind - I commented out the default constructor and I get the same error

  6. #6
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Undefined Reference to vtable

    I was getting the same error with a custom table model (but only one out of 5, go figure).

    Try the following:

    1) See if it compiles correctly if you take out the Q_OBJECT define (and you may want to comment out the signals and slots as well).
    2) If it compiles, then the moc compiler could be causing problems, so see if the following fixes it:
    a) Specify CONFIG += release in the pro file.
    b) Put the destructor definition in the header file (ie. ~MyClass(){})

    This worked for me compiling with Qt 4.5.3. If you want to build debug as well just change CONFIG += release build_all -> this builds the release version first, then the debug version, which seems to help.

    EDIT: I forgot to add that Jaus_neighbor must inherit from QObject.
    Last edited by bmhautz; 24th February 2010 at 20:15.

  7. #7
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Undefined Reference to vtable

    Quote Originally Posted by ctote View Post
    in addition, i tried adding
    Qt Code:
    1. udpSocket = new QUdpSocket(this);
    To copy to clipboard, switch view to plain text mode 
    to the Jaus_neighbor constructor and keep getting the following error:

    error: no matching function for call to 'QUdpSocket::QUdpSocket(Jaus_neighbor* const)
    QUdpSocket::QUdpSocket(QObject* parent = 0) requires that Jaus_neighbor be a subclass of QObject.

  8. #8
    Join Date
    Jan 2010
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Undefined Reference to vtable

    commented it out and i still get the same error

  9. #9
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Qt Undefined Reference to vtable

    Post the last version of the code of jaus.h and jaus.cpp
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  10. #10
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Undefined Reference to vtable

    It may be useful to post updated code with a note of what changed and the specific errors you are getting.

  11. #11
    Join Date
    Jan 2010
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Undefined Reference to vtable

    Here's my updated code:
    Qt Code:
    1. #ifndef JAUS_H
    2. #define JAUS_H
    3.  
    4. #include <QtNetwork>
    5. #include <QUdpSocket>
    6. #include <QObject>
    7.  
    8. struct Jaus_header
    9. {
    10. unsigned short message_properties;
    11. unsigned short command_code;
    12. QByteArray dest_id[4];
    13. QByteArray source_id[4];
    14. unsigned short data_control;
    15. unsigned short sequence_num;
    16. };
    17.  
    18. class Jaus_neighbor : public QObject
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. void handshake();
    24. void capabilities_discover();
    25. void sys_management();
    26. void close_conn();
    27.  
    28. private:
    29. Jaus_header head;
    30.  
    31. QUdpSocket *udpSocket;
    32.  
    33. private slots:
    34. Jaus_neighbor();
    35.  
    36. void prepare_header();
    37. void read_jheader();
    38. void send_jheader();
    39.  
    40. void do_command();
    41. void startBroadcasting();
    42. void stopBroadcasting();
    43. void sendDatagram();
    44. //void verifyDatagram();
    45. };
    46.  
    47. #endif // JAUS_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "jaus.h"
    2.  
    3. Jaus_neighbor::Jaus_neighbor()
    4. {
    5. udpSocket = new QUdpSocket(this);
    6. for (int i = 0; i < 4; i++)
    7. {
    8. head.dest_id[i] = 0;
    9. head.source_id[i] = 0;
    10. }
    11. head.sequence_num = 0;
    12. head.data_control = 0;
    13. head.message_properties = 0;
    14. head.command_code = 0;
    15. }
    16.  
    17. void Jaus_neighbor::startBroadcasting()
    18. {
    19.  
    20. }
    21.  
    22. void Jaus_neighbor::stopBroadcasting()
    23. {
    24.  
    25. }
    26.  
    27. void Jaus_neighbor::sendDatagram()
    28. {
    29. //udpSocket->writeDatagram(message, QHostAddress::LocalHost, 9000);
    30. }
    31.  
    32.  
    33. void Jaus_neighbor::handshake()
    34. {
    35. Jaus_neighbor::startBroadcasting();
    36. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <iostream>
    3. #include "jaus.h"
    4. using namespace std;
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10. cout << "Initializing Jaus Connections..." << flush;
    11. //Jaus_neighbor cop;
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    I'm still getting the warnings -- but more importantly I think, is I'm getting the following error also: error: collect2: ld returned 1 exit status
    If I comment out my default constructor, the app compiles fine.

  12. #12
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Qt Undefined Reference to vtable

    You have your constructor in private slots, that means when you declare an Jaus_neighbor object you can't access the constructor cause it's private. So move it to public.
    Also you're getting undefined references for the functions prepare_header(), read_jheader(), send_jheader() and do_command() because there aren't implementation for those functions in the jaus.cpp.
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  13. #13
    Join Date
    Jan 2010
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Undefined Reference to vtable

    Thanks for the info -- I moved the constructor to public: but I still get the same error

  14. #14
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Qt Undefined Reference to vtable

    I copy pasted the code you gave. I moved the constructor to public, provided the implementation for the functions I told in the previous post ( put in the .cpp file eg.
    void Jaus_neighbor:repare_header() {} and so on) and compiled with success!
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  15. #15
    Join Date
    Jan 2010
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Undefined Reference to vtable

    well that's just not fair then!

    here's my new .cpp file (and i moved the constructor):
    Qt Code:
    1. #include "jaus.h"
    2.  
    3. Jaus_neighbor::Jaus_neighbor()
    4. {
    5. udpSocket = new QUdpSocket(this);
    6. for (int i = 0; i < 4; i++)
    7. {
    8. header.dest_id[i] = 0;
    9. header.source_id[i] = 0;
    10. }
    11. header.sequence_num = 0;
    12. header.data_control = 0;
    13. header.message_properties = 0;
    14. header.command_code = 0;
    15. }
    16.  
    17. void Jaus_neighbor::startBroadcasting()
    18. {
    19.  
    20. }
    21.  
    22. void Jaus_neighbor::stopBroadcasting()
    23. {
    24.  
    25. }
    26.  
    27. void Jaus_neighbor::sendDatagram()
    28. {
    29. //udpSocket->writeDatagram(message, QHostAddress::LocalHost, 9000);
    30. }
    31.  
    32.  
    33. void Jaus_neighbor::handshake()
    34. {
    35. Jaus_neighbor::startBroadcasting();
    36. }
    37.  
    38. void Jaus_neighbor::prepare_header(){}
    39. void Jaus_neighbor::read_jheader(){}
    40. void Jaus_neighbor::send_jheader(){}
    41. void Jaus_neighbor::do_command(){}
    To copy to clipboard, switch view to plain text mode 

    Same error though

  16. #16
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Qt Undefined Reference to vtable

    What are the differences with the code you'll find in the .zip??
    Also did you rerun qmake?
    Attached Files Attached Files
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  17. #17
    Join Date
    Jan 2010
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Undefined Reference to vtable

    Ok, here's what happened -- I ran your code and it compiled fine but no command line popped up, so I ran it again... now I get the same error every time with your code. I tried to run qmake, but it keeps saying unknown command.

    This is frustrating

    Also, I just noticed our .pro files are different... not sure if that matters:

    mine:
    Qt Code:
    1. # -------------------------------------------------
    2. # Project created by QtCreator 2010-02-24T13:23:24
    3. # -------------------------------------------------
    4. QT += network
    5. QT -= gui
    6. TARGET = jausCommandLine
    7. CONFIG += console
    8. CONFIG -= app_bundle
    9. TEMPLATE = app
    10. SOURCES += main.cpp \
    11. jaus.cpp
    12. HEADERS += jaus.h
    To copy to clipboard, switch view to plain text mode 

    yours:
    Qt Code:
    1. QT += network
    2.  
    3. TARGET = jaus
    4. TEMPLATE = app
    5.  
    6.  
    7. SOURCES += main.cpp\
    8. jaus.cpp
    9.  
    10. HEADERS += jaus.h
    To copy to clipboard, switch view to plain text mode 
    Last edited by ctote; 24th February 2010 at 21:40.

  18. #18
    Join Date
    Jan 2010
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Undefined Reference to vtable

    Just changed my .pro file for yours (except I added the CONFIG section for my command line) -- it's working great now. Why? I'm very new to Qt and don't quite understand the requirements in the .pro file (as well as the requirements for object creation e.g., Q_OBJECT).

  19. #19
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Undefined Reference to vtable

    Typically, if you get "Undefined Reference to vtable" whilst building a project you need to ensure MOC is being run. The easiest way to ensure this is to simply run qmake from the QtCreator menu bar and then rebuild the project. Q_OBJECT is only required if your class needs signals or slots - these are then read by MOC and parsed into another file that it creates and automatically adds to your project. Without this file, you get the vtable error.

Similar Threads

  1. undefined reference to vtable in Threads.
    By prasanth.nvs in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 20th February 2009, 10:19
  2. Undefined reference to 'vtable for XXX'
    By Sheng in forum Qt Programming
    Replies: 4
    Last Post: 17th October 2008, 15:59
  3. undefined reference to vtable
    By renjithmamman in forum Qt Programming
    Replies: 5
    Last Post: 2nd July 2006, 04:23
  4. Replies: 2
    Last Post: 30th June 2006, 18:42

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.