PDA

View Full Version : Qt Undefined Reference to vtable



ctote
24th February 2010, 20:13
Hey guys,

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

Here's my code:

#ifndef JAUS_H
#define JAUS_H

#include <QtNetwork>

struct Jaus_header
{
unsigned short message_properties;
unsigned short command_code;
QByteArray dest_id[4];
QByteArray source_id[4];
unsigned short data_control;
unsigned short sequence_num;
};

class Jaus_neighbor
{
Q_OBJECT

public:
void handshake();
void capabilities_discover();
void sys_management();
void close_conn();

private:
Jaus_header head;

unsigned short command;
QByteArray dest[4];
QByteArray source[4];
unsigned short control;
unsigned short seq_num;

QUdpSocket *udpSocket;

private slots:
void read_jheader();
void send_jheader();
void set_data();

void do_command();
void startBroadcasting();
void stopBroadcasting();
void sendDatagram();
//void verifyDatagram();
};

#endif // JAUS_H



#include "jaus.h"

void Jaus_neighbor::startBroadcasting()
{

}

void Jaus_neighbor::stopBroadcasting()
{

}

void Jaus_neighbor::sendDatagram()
{
//udpSocket->writeDatagram(message, QHostAddress::LocalHost, 9000);
}


void Jaus_neighbor::handshake()
{
Jaus_neighbor::startBroadcasting();
}




#include <QtCore/QCoreApplication>
#include <iostream>
#include "jaus.h"
using namespace std;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

cout << "Initializing Jaus Connections..." << flush;
Jaus_neighbor cop;

return a.exec();
}

Please help!

Archimedes
24th February 2010, 20:25
What happens if you include QObject and make Jaus_neighbor a subclass of QObject?

ctote
24th February 2010, 20:37
in addition, i tried adding
udpSocket = new QUdpSocket(this); to the Jaus_neighbor constructor and keep getting the following error:

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

ctote
24th February 2010, 20:42
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()


Jaus_neighbor::Jaus_neighbor()
{
udpSocket = new QUdpSocket(this);
for (int i = 0; i < 4; i++)
{
head.dest_id[i] = 0;
head.source_id[i] = 0;
}
head.sequence_num = 0;
head.data_control = 0;
head.message_properties = 0;
head.command_code = NULL;
}

ctote
24th February 2010, 20:45
Nevermind - I commented out the default constructor and I get the same error

bmhautz
24th February 2010, 21:06
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.

bmhautz
24th February 2010, 21:10
in addition, i tried adding
udpSocket = new QUdpSocket(this); 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.

ctote
24th February 2010, 21:14
commented it out and i still get the same error

Archimedes
24th February 2010, 21:17
Post the last version of the code of jaus.h and jaus.cpp

bmhautz
24th February 2010, 21:18
It may be useful to post updated code with a note of what changed and the specific errors you are getting.

ctote
24th February 2010, 21:42
Here's my updated code:

#ifndef JAUS_H
#define JAUS_H

#include <QtNetwork>
#include <QUdpSocket>
#include <QObject>

struct Jaus_header
{
unsigned short message_properties;
unsigned short command_code;
QByteArray dest_id[4];
QByteArray source_id[4];
unsigned short data_control;
unsigned short sequence_num;
};

class Jaus_neighbor : public QObject
{
Q_OBJECT

public:
void handshake();
void capabilities_discover();
void sys_management();
void close_conn();

private:
Jaus_header head;

QUdpSocket *udpSocket;

private slots:
Jaus_neighbor();

void prepare_header();
void read_jheader();
void send_jheader();

void do_command();
void startBroadcasting();
void stopBroadcasting();
void sendDatagram();
//void verifyDatagram();
};

#endif // JAUS_H



#include "jaus.h"

Jaus_neighbor::Jaus_neighbor()
{
udpSocket = new QUdpSocket(this);
for (int i = 0; i < 4; i++)
{
head.dest_id[i] = 0;
head.source_id[i] = 0;
}
head.sequence_num = 0;
head.data_control = 0;
head.message_properties = 0;
head.command_code = 0;
}

void Jaus_neighbor::startBroadcasting()
{

}

void Jaus_neighbor::stopBroadcasting()
{

}

void Jaus_neighbor::sendDatagram()
{
//udpSocket->writeDatagram(message, QHostAddress::LocalHost, 9000);
}


void Jaus_neighbor::handshake()
{
Jaus_neighbor::startBroadcasting();
}


#include <QtCore/QCoreApplication>
#include <iostream>
#include "jaus.h"
using namespace std;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

cout << "Initializing Jaus Connections..." << flush;
//Jaus_neighbor cop;

return a.exec();
}

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.

Archimedes
24th February 2010, 21:57
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.

ctote
24th February 2010, 22:02
Thanks for the info -- I moved the constructor to public: but I still get the same error

Archimedes
24th February 2010, 22:09
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::prepare_header() {} and so on) and compiled with success!

ctote
24th February 2010, 22:22
well that's just not fair then!

here's my new .cpp file (and i moved the constructor):

#include "jaus.h"

Jaus_neighbor::Jaus_neighbor()
{
udpSocket = new QUdpSocket(this);
for (int i = 0; i < 4; i++)
{
header.dest_id[i] = 0;
header.source_id[i] = 0;
}
header.sequence_num = 0;
header.data_control = 0;
header.message_properties = 0;
header.command_code = 0;
}

void Jaus_neighbor::startBroadcasting()
{

}

void Jaus_neighbor::stopBroadcasting()
{

}

void Jaus_neighbor::sendDatagram()
{
//udpSocket->writeDatagram(message, QHostAddress::LocalHost, 9000);
}


void Jaus_neighbor::handshake()
{
Jaus_neighbor::startBroadcasting();
}

void Jaus_neighbor::prepare_header(){}
void Jaus_neighbor::read_jheader(){}
void Jaus_neighbor::send_jheader(){}
void Jaus_neighbor::do_command(){}

Same error though :(

Archimedes
24th February 2010, 22:30
What are the differences with the code you'll find in the .zip??
Also did you rerun qmake?

ctote
24th February 2010, 22:35
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 :confused:

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

mine:

# -------------------------------------------------
# Project created by QtCreator 2010-02-24T13:23:24
# -------------------------------------------------
QT += network
QT -= gui
TARGET = jausCommandLine
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
jaus.cpp
HEADERS += jaus.h


yours:


QT += network

TARGET = jaus
TEMPLATE = app


SOURCES += main.cpp\
jaus.cpp

HEADERS += jaus.h

ctote
24th February 2010, 22:43
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).

squidge
24th February 2010, 23:24
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.