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. Using Object in another function within the class

Using Object in another function within the class

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 378 Views 2 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.
  • M Offline
    M Offline
    Metshiix
    wrote on last edited by
    #1

    hello guys! i have a problem, i want to use the server and client object that i create in connectUDP function in my updateChat function but the compiler says that client and server are undeclared. how can i fix this (line 41, 42, 43 error)

    #include "myudp.h"
    #include <QDebug>
    
    MyUDP::MyUDP(QObject *parent)
        : QObject{parent}
    {
        socket = new QUdpSocket(this);
        connect(socket, &QUdpSocket::readyRead, this, &MyUDP::Read);
    }
    
    void MyUDP::initServer()
    {
        socket->bind(QHostAddress::LocalHost, 7755);
    }
    
    void MyUDP::sendMessage(double value)
    {
        qDebug() << value; //ok but cant read
        QByteArray Data;
        Data.append(value);
        socket->writeDatagram(Data, QHostAddress::LocalHost, 7755);
    }
    
    void MyUDP::Read()
    {
        m_Recieved.resize(socket->pendingDatagramSize()); //damit keine infos verloren gehen, wenn daten zu lang sind
        socket->readDatagram(m_Recieved.data(), m_Recieved.size(), &m_sender, &m_senderPort); //readDatagramm befüllt sozusagen das recieved
    
        emit giveReadValues(m_Recieved, m_sender, m_senderPort);
    }
    
    void MyUDP::connectUDP()
    {
        MyUDP server;
        MyUDP client;
        server.initServer();
    }
    
    void MyUDP::updateChat(double pm10, double pm25)
    {
        client->sendMessage(pm10);
        client->sendMessage(pm25);
        server->Read();
    }
    

    hope someone can help me. thanks!

    Christian EhrlicherC M Pl45m4P 3 Replies Last reply
    0
    • M Metshiix

      hello guys! i have a problem, i want to use the server and client object that i create in connectUDP function in my updateChat function but the compiler says that client and server are undeclared. how can i fix this (line 41, 42, 43 error)

      #include "myudp.h"
      #include <QDebug>
      
      MyUDP::MyUDP(QObject *parent)
          : QObject{parent}
      {
          socket = new QUdpSocket(this);
          connect(socket, &QUdpSocket::readyRead, this, &MyUDP::Read);
      }
      
      void MyUDP::initServer()
      {
          socket->bind(QHostAddress::LocalHost, 7755);
      }
      
      void MyUDP::sendMessage(double value)
      {
          qDebug() << value; //ok but cant read
          QByteArray Data;
          Data.append(value);
          socket->writeDatagram(Data, QHostAddress::LocalHost, 7755);
      }
      
      void MyUDP::Read()
      {
          m_Recieved.resize(socket->pendingDatagramSize()); //damit keine infos verloren gehen, wenn daten zu lang sind
          socket->readDatagram(m_Recieved.data(), m_Recieved.size(), &m_sender, &m_senderPort); //readDatagramm befüllt sozusagen das recieved
      
          emit giveReadValues(m_Recieved, m_sender, m_senderPort);
      }
      
      void MyUDP::connectUDP()
      {
          MyUDP server;
          MyUDP client;
          server.initServer();
      }
      
      void MyUDP::updateChat(double pm10, double pm25)
      {
          client->sendMessage(pm10);
          client->sendMessage(pm25);
          server->Read();
      }
      

      hope someone can help me. thanks!

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Metshiix said in Using Object in another function within the class:

      MyUDP server;
      MyUDP client;
      server.initServer();
      

      Basic c++ - what's the lifetime of those two variables?

      client->sendMessage(pm10);
      server->Read();

      dito - where are those pointers initialized?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • M Metshiix

        hello guys! i have a problem, i want to use the server and client object that i create in connectUDP function in my updateChat function but the compiler says that client and server are undeclared. how can i fix this (line 41, 42, 43 error)

        #include "myudp.h"
        #include <QDebug>
        
        MyUDP::MyUDP(QObject *parent)
            : QObject{parent}
        {
            socket = new QUdpSocket(this);
            connect(socket, &QUdpSocket::readyRead, this, &MyUDP::Read);
        }
        
        void MyUDP::initServer()
        {
            socket->bind(QHostAddress::LocalHost, 7755);
        }
        
        void MyUDP::sendMessage(double value)
        {
            qDebug() << value; //ok but cant read
            QByteArray Data;
            Data.append(value);
            socket->writeDatagram(Data, QHostAddress::LocalHost, 7755);
        }
        
        void MyUDP::Read()
        {
            m_Recieved.resize(socket->pendingDatagramSize()); //damit keine infos verloren gehen, wenn daten zu lang sind
            socket->readDatagram(m_Recieved.data(), m_Recieved.size(), &m_sender, &m_senderPort); //readDatagramm befüllt sozusagen das recieved
        
            emit giveReadValues(m_Recieved, m_sender, m_senderPort);
        }
        
        void MyUDP::connectUDP()
        {
            MyUDP server;
            MyUDP client;
            server.initServer();
        }
        
        void MyUDP::updateChat(double pm10, double pm25)
        {
            client->sendMessage(pm10);
            client->sendMessage(pm25);
            server->Read();
        }
        

        hope someone can help me. thanks!

        M Offline
        M Offline
        Metshiix
        wrote on last edited by
        #3

        @Metshiix that "->" is just false, i tried something out. Has no meaning in the code. Those arent pointers, just errors.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          The question stays valid. You create client and server on the stack in a function hence what are the scope of these variables ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • M Metshiix

            hello guys! i have a problem, i want to use the server and client object that i create in connectUDP function in my updateChat function but the compiler says that client and server are undeclared. how can i fix this (line 41, 42, 43 error)

            #include "myudp.h"
            #include <QDebug>
            
            MyUDP::MyUDP(QObject *parent)
                : QObject{parent}
            {
                socket = new QUdpSocket(this);
                connect(socket, &QUdpSocket::readyRead, this, &MyUDP::Read);
            }
            
            void MyUDP::initServer()
            {
                socket->bind(QHostAddress::LocalHost, 7755);
            }
            
            void MyUDP::sendMessage(double value)
            {
                qDebug() << value; //ok but cant read
                QByteArray Data;
                Data.append(value);
                socket->writeDatagram(Data, QHostAddress::LocalHost, 7755);
            }
            
            void MyUDP::Read()
            {
                m_Recieved.resize(socket->pendingDatagramSize()); //damit keine infos verloren gehen, wenn daten zu lang sind
                socket->readDatagram(m_Recieved.data(), m_Recieved.size(), &m_sender, &m_senderPort); //readDatagramm befüllt sozusagen das recieved
            
                emit giveReadValues(m_Recieved, m_sender, m_senderPort);
            }
            
            void MyUDP::connectUDP()
            {
                MyUDP server;
                MyUDP client;
                server.initServer();
            }
            
            void MyUDP::updateChat(double pm10, double pm25)
            {
                client->sendMessage(pm10);
                client->sendMessage(pm25);
                server->Read();
            }
            

            hope someone can help me. thanks!

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @Metshiix said in Using Object in another function within the class:

            how can i fix this (line 41, 42, 43 error)

            Look at your variable socket and the places where you are using it.
            Then look at server and client again. Able to spot any difference?!
            Also, what @Christian-Ehrlicher and @SGaist have said.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            1

            • Login

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