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. Passing Pointer to QByteArray
Qt 6.11 is out! See what's new in the release blog

Passing Pointer to QByteArray

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.7k 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
    jediengineer
    wrote on last edited by
    #1

    I'm working on a function that needs a pointer to a QByteArray that stores a UDP datagram. So fat I've tried passing the pointer like this with no luck:

    @
    void my_func::udp_read();
    {
    QByteArray buffer;
    udpSocket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);

    process_buffer(QByteArray * buffer);
    ...
    ...
    return;
    

    }
    void process_buffer(QByteArray * incoming);
    {
    QByteArray in_buffer = (QByteArray)incoming;
    ...
    ...
    return;
    }
    @

    So far, all I get is a compiler error that says " Expected primary-expression before "(asterisk)" token" at the function call line, and " No matching function for call to 'QByteArray::QByteArray(QByteArray*&)' QByteArray buffer = (QByteArray)incoming;" at that line in the error.

    Can someone point me in the right direction (no pun intended) and let me know what I'm missing?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bipll
      wrote on last edited by
      #2

      The correct C++ syntax of the call would be @ process_buffer(&buffer);@

      BTW, you don't have to return; on the last line of void-returning function, inside these functions return is only needed as the means of preliminary escape, so the control would jump out of the function body.

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

        Hey! Thanks for the info, that cleared it up. If it works (I'll know by the end of the day) I'll let you know. So far so good! Thanks again!!

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          This
          @
          QByteArray in_buffer = (QByteArray)incoming;
          @
          won't wok. You can't cast pointer to a class type (well, you can, but it's nonsense).

          Just make your function accept a reference and don't play with pointers when there's no need for it.

          @
          void my_func::udp_read() //Don't put ; here !!
          {
          QByteArray buffer;
          udpSocket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
          process_buffer(buffer);
          ...
          }
          void process_buffer(const QByteArray& incoming);
          {
          //this is redundant, just work directly with "incoming"
          //QByteArray in_buffer = (QByteArray)incoming;
          doSomething(incoming);
          ...
          }
          @

          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