UDP Class compile error
Solved
General and Desktop
-
I have a UDP Class
udp.h#include <QObject> #include <QUdpSocket> class MyUDP : public QObject { Q_OBJECT public: explicit MyUDP(QObject *parent = nullptr); void Send(QString data, QString remote_ip, quint16 remote_port); void Start(QString ip, quint16 port); signals: public slots: void readyRead(QByteArray buffer); private: QUdpSocket *socket; };
udp.cpp
#include "udp.h" MyUDP::MyUDP(QObject *parent) : QObject(parent) { // create a QUDP socket socket = new QUdpSocket(this); } void MyUDP::Start(QString local_ip, quint16 local_port) { socket->bind(QHostAddress(local_ip), local_port); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); } void MyUDP::Send(QString data, QString remote_ip, quint16 remote_port) { QByteArray Data; Data.append(data); socket->writeDatagram(Data, QHostAddress(remote_ip), remote_port); } void MyUDP::readyRead(QByteArray buffer) { // when data comes in buffer.resize(socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort); qDebug() << "Message from: " << sender.toString(); qDebug() << "Message port: " << senderPort; qDebug() << "Message: " << buffer; }
When I duild I get
debug/udp.o:udp.cpp:(.rdata$.refptr._ZTV5MyUDP[.refptr._ZTV5MyUDP]+0x0): undefined reference to `vtable for MyUDP'
collect2.exe: error: ld returned 1 exit statusWhat may be a problem?
-
@jenya7
Hi
Sometimes I have the same issue.
Normally I delete the build folder via the file explorer, but sometimes this operation doesn't fix this problem.
I don't know why, but when I create a new class I sometimes see this issue.
It seems the compiler has a problem with Q_OBJECT macro, indeed the macro seems doesn't be recognized (not change color in the QtCreator editor), when it appears I remove the macro and write again.