2012-11-16 103 views
14

我對我所產生的信號的一類服務器接合(QString的名稱)。我把它在一個叫加盟(QString的名稱)的功能,但是我得到的錯誤QT信號未定義參考誤差

Server.o:在功能Server::join(QString)': Server.cpp:(.text+0x48): undefined reference to 服務器::加入(QString的)」 collect2:LD返回1個退出狀態

這是我的頭文件看起來像:

#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 

,這是我的cpp文件:

#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); 
} 

回答

34

在你的類聲明的開頭,你應該有宏觀Q_OBJECT,並確保從一些QObject後代繼承。

+1

而且還從QObject繼承,我想呢? –

+2

我做到了,但這次我得到一個額外的錯誤,現在「未定義引用的vtable服務器」 我上課的時候是這樣的: 類服務器:公共QObject的 { Q_OBJECT 市民: 。 。 。 } 我甚至乾淨的項目,並做了重建所有。 – Amre

+8

@Amre運行「make distclean」,然後再次「qmake」。這將重建您的Makefiles,以便moc規則保持最新狀態。在這種情況下,「乾淨」是不夠的。 –

3

除無論是在this answer提到的,還要確保運行qmake。即:

Build > Run qmake 

這應該修正這些錯誤。