22

I have a class server for which I have created a signal joined(QString name). I call it in a function called join(QString name), however I'm getting the error

Server.o: In function Server::join(QString)': Server.cpp:(.text+0x48): undefined reference to Server::joined(QString)' collect2: ld returned 1 exit status

This is what my header file looks like:

#ifndef SERVER_H
#define SERVER_H

#include <QString>
#include <mqueue.h>
#include <QVector>
#include <QStringList>
#include "../src/messages.h"

class Server
{
public:
    Server();
    void start();
private:
    void join(QString name);
    char buf[MSG_SIZE], msgSend[MSG_SIZE];
    QVector<mqd_t> mq_external;
    QVector<QString> users;
    mqd_t mq_central;
    struct mq_attr attr;


signals:
    void joined(QString name);

};

#endif // SERVER_H

and this is my cpp file:

#include "Server.h"

using namespace std;

Server::Server()
{
}

void Server::start(){

    attr.mq_maxmsg = 100;
    attr.mq_msgsize = MSG_SIZE;
    attr.mq_flags = 0;

    mq_unlink(CENTRALBOX);
    mq_central = mq_open(CENTRALBOX, O_RDONLY | O_CREAT, S_IRWXU, &attr);
    while(1)
    {
        int tempMsgVal = mq_receive(mq_central, buf, MSG_SIZE, 0);
        if(tempMsgVal != -1){
            QString tempS = buf;
            QStringList tempSL = tempS.split(":");
            if(tempSL.size() == 2 && tempSL.at(0) == "started")
            {
                int x = 0;
                bool exists = false;
                for(int i = 0; i < mq_external.size(); i++)
                {
                    x = QString::compare(tempSL[1], users.at(i), Qt::CaseInsensitive);
                    if(x == 0)
                    {
                        exists = true;
                        break;
                    }
                }

                if(!exists)
                {
                    sprintf(buf,"joined");
                    QString tempS1 = tempSL[1] + "new";
                    QByteArray byteArray = tempS1.toUtf8();
                    const char* tempr = byteArray.constData();
                    mqd_t tempMQ = mq_open(tempr, O_RDWR);
                    int tempI = mq_send(tempMQ, buf, strlen(buf), 0);
                }
                else
                {
                    sprintf(buf,"invalidname");
                    QString tempS1 = tempSL[1] + "new";
                    QByteArray byteArray = tempS1.toUtf8();
                    const char* tempr = byteArray.constData();
                    mqd_t tempMQ = mq_open(tempr, O_RDWR);
                    int tempI = mq_send(tempMQ, buf, strlen(buf), 0);
                }//Endelse
            }//Endif
        }//Endif

    }//Endwhile
}

void Server::join(QString name)
{
    emit joined(name);
}
Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
Amre
  • 1,630
  • 8
  • 29
  • 41

2 Answers2

59

At the beginning of your class declaration you should have the macro Q_OBJECT and be sure to inherit from some QObject descendant.

Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
  • 1
    And also inherit from QObject, I think? – Nikos C. Nov 16 '12 at 02:16
  • 4
    I did it, but this time I'm getting an additional error "undefined reference to vtable for server" The beginning of my class now looks like this: class Server : public QObject { Q_OBJECT public: . . . } I even clean the project and did a rebuild all. – Amre Nov 16 '12 at 02:23
  • 12
    @Amre Run "make distclean", and then "qmake" again. This will rebuild your Makefiles so that the moc rules are up to date. A "make clean" is not enough in this case. – Nikos C. Nov 16 '12 at 02:29
  • 13
    for those who read on this who aren't using make, it might be worth a note to go into your build directory and delete all files, especially if you are using an IDE like qt creator because you can clean and rebuild to your hearts content but if you dont go in and delete those files, you'll never get rid of that vtable error. – osirisgothra Dec 16 '13 at 19:17
  • see also the [answer to Qt “signal undefined reference error” after inheriting from QObject](http://stackoverflow.com/a/19173099/427158) for details on the needed cleanup. – maxschlepzig Mar 30 '15 at 09:37
22

Apart from whatever is mentioned in this answer, also make sure to:

click 'Build' option > Run 'qmake'

That should fix the pending errors as well.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 1
    This one that worked for me. My class had all the required Qt traits (inherits from QObject, etc.) but the error persisted. My class didn't have a .ui though. – Paulo Carvalho Mar 20 '19 at 10:51