Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. UDP Class compile error
Forum Updated to NodeBB v4.3 + New Features

UDP Class compile error

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 272 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jenya7
    wrote on last edited by jenya7
    #1

    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 status

    What may be a problem?

    CP71C 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Try cleaning your build folder and rerun qmake/cmake

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • J Offline
        J Offline
        jenya7
        wrote on last edited by
        #3

        Did Clean then Rebuild - the same problem.

        1 Reply Last reply
        0
        • J jenya7

          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 status

          What may be a problem?

          CP71C Offline
          CP71C Offline
          CP71
          wrote on last edited by
          #4

          @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.

          J 1 Reply Last reply
          3
          • CP71C CP71

            @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.

            J Offline
            J Offline
            jenya7
            wrote on last edited by
            #5

            @CP71
            Thank you. Deleting the build folder helped.

            CP71C 1 Reply Last reply
            1
            • J jenya7

              @CP71
              Thank you. Deleting the build folder helped.

              CP71C Offline
              CP71C Offline
              CP71
              wrote on last edited by
              #6

              @jenya7
              You are welcome!

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved