qt信号未定义的参考错误

Amr*_*mre 16 c++ qt signals-slots qt-signals

我有一个类服务器,我已经创建了一个加入的信号(QString名称).我在一个名为join(QString name)的函数中调用它,但是我收到了错误

Server.o:在函数Server::join(QString)': Server.cpp:(.text+0x48): undefined reference to Server :: joined(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
Run Code Online (Sandbox Code Playgroud)

这是我的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);
}
Run Code Online (Sandbox Code Playgroud)

Arn*_*nce 45

在类声明的开头,你应该有宏,Q_OBJECT并确保从一些QObject后代继承.

  • 对于那些阅读过不使用make的人来说,进入构建目录并删除所有文件可能值得注意,特别是如果你使用的是像qt creator这样的IDE,因为你可以清理和重建你的内容但如果你不进去删除那些文件,你永远不会摆脱那个vtable错误. (12认同)
  • @Amre运行"make distclean",然后再"qmake".这将重建您的Makefile,以便moc规则是最新的.在这种情况下,"干净"是不够的. (9认同)
  • 我做到了,但这次我得到一个额外的错误"对服务器的vtable的未定义引用"我的类的开头现在看起来像这样:class Server:public QObject {Q_OBJECT public :. ..我甚至清理了项目并进行了全部重建. (3认同)

iam*_*ind 14

除了此答案中提到的内容外,还请确保运行qmake。即:

Build > Run qmake
Run Code Online (Sandbox Code Playgroud)

那应该解决这些错误。