C++ qt 信号未定义参考错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13409343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
qt signal undefined reference error
提问by Amre
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
我有一个类服务器,我为它创建了一个信号加入(QString 名称)。我在一个名为 join(QString name) 的函数中调用它,但是我收到错误
Server.o: In function
Server::join(QString)': Server.cpp:(.text+0x48): undefined reference to
Server::joined(QString)' collect2: ld returned 1 exit status
Server.o: 在函数
Server::join(QString)': Server.cpp:(.text+0x48): undefined reference to
Server::joined(QString)' collect2: ld 返回 1 退出状态
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:
这是我的 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);
}
回答by Arnold Spence
At the beginning of your class declaration you should have the macro Q_OBJECT
and be sure to inherit from some QObject
descendant.
在您的类声明的开头,您应该拥有宏Q_OBJECT
并确保从某个QObject
后代继承。
回答by iammilind
Apart from whatever is mentioned in this answer, also make sure to run qmake
. i.e.:
除了这个答案中提到的任何内容之外,还要确保运行qmake
. IE:
Build > Run qmake
That should fix these errors.
那应该修复这些错误。