Search code examples
c++qtqbytearray

Appending one QByteArray to another QByteArray


I wrote the following function, which is supposed to allow me to append some input QByteArray data to a private QByteArray inside of my class:

void addQByteArray(QByteArray* array)
{
    p_commandArray.append(array);
}

My understanding is that the append function should also accept a QByteArray as a parameter, but I'm getting this error when I try to compile:

error: invalid conversion from ‘QByteArray*’ to ‘char’ [-fpermissive] p_commandArray.append(array); ^

What am I doing wrong here?


Solution

  • Yes QByteArray::append has an overload that you can pass a QByteArray (more precicelsy a const QByteArray&), but what you attempt to pass is a pointer to QByteArray.

    Either

    void addQByteArray(QByteArray& array)
    {
        p_commandArray.append(array);
    }
    

    or

    void addQByteArray(QByteArray* array)
    {
        p_commandArray.append(*array);
    }
    

    Prefer the first (passing a reference), because using pointers only makes sense when a nullptr is a valid parameter, but when you pass a nullptr you shall not dereference it.

    The error message you get is due to another overload of append that takes a char as parameter.