Passing Pointer to QByteArray



  • 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?



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



  • 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!!


  • Moderators

    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);
    ...
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.