views:

352

answers:

5

This is my header:

#ifndef BARELYSOCKET_H
#define BARELYSOCKET_H

#include <QObject>
//! The First Draw of the BarelySocket!

class BarelySocket: public QObject
{
    Q_OBJECT

public:
    BarelySocket();
public slots:
    void sendMessage(Message aMessage);
signals:
    void reciveMessage(Message aMessage);

private:



 //   QVector<Message> reciveMessages;
};

#endif // BARELYSOCKET_H

This is my class:

#include <QTGui>
#include <QObject>
#include "type.h"
#include "client.h"
#include "server.h"

#include "barelysocket.h"

BarelySocket::BarelySocket()
{


    //this->reciveMessages.clear();
        qDebug("BarelySocket::BarelySocket()");
}


void BarelySocket::sendMessage(Message aMessage)
{

}


void BarelySocket::reciveMessage(Message aMessage)
{

}

I get the Linker Problem : undefined reference to 'vtable for barelySocket'

This should mean, i have a virtual Function not implemented. But as you can see, there is non.

I comment the vector cause that should solve the Problem, but i does not.

The Message is a complex struct, but even converting it to int did not solve it.

I searched Mr G but he could not help me.

Thank you for your support,

Thomas

+4  A: 

From experience: oftentimes a qmake && make clean && make helps. I personally perceive that sometimes the change discovery / caching effects / whatever-I-don't-know xxxxx. I can't say why, but it's the first thing I do when I encounter this kind of error.

btw. there's a typo at > recive <

You forgot to call the QObject constructor in your constructor (in the initializer list). (It doesn't resolve the error though)

Ronny
This is especially useful when the file has existed but didn't have any Q_OBJECT references in it when qmake was run. qmake then doesn't believe it needs to run moc and you end up with vtable errors. The make clean isn't always necessary, but is when certain structural changes are made.
Kaleb Pederson
Also make sure that barelysocket.h is in HEADERS section in your pro file.
chalup
+1  A: 

Signals must not have an implementation (This wil be generated by Qt). Remove the reciveMessage implementation from your .cpp file. This may solve your problem.

An other thing I've seen: Since the BarelySocket class inherit from QObject it must have a virtual destructor to avoid problem during destruction. This must be done for all class that inherit from an other class.

Patrice Bernassola
Removing receiveMessage implementation is necessary, but it would rather cause "multiply defined symbol" error.If base class (QObject in this case) has virtual destructor, then destructors in all derived classes are automatically virtual. So that's not an issue here.
chalup
+1  A: 

I ran into this error after I created a little class inside a small "main.cpp" file that I had created to test something.

After futzing for an hour or so, I finally moved that class out of main.cpp and into a standalone hpp file, updated the .pro (project) file and the project then built perfectly fine. That may not have been the issue here but I figured it would be useful information anyway.

David
+2  A: 

Any time you add a new call to the Q_OBJECT macro, you need to run qmake again. The vtables issue you're referring to is directly related to that.

Just run qmake and you should be good to go assuming there are no other issues in your code.

Michael
A: 

When you derive a class from QOBject (and use the Q_OBJECT macro), don't forget to specifically define and create both the constructor and destructor classes. It's not enough to use the compiler default constructor/destructors. The advice on cleaning/running qmake (and clearing out your moc_ files) still applies. This fixed my similar problem.

Pete