-1

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?

Jason O
  • 753
  • 9
  • 28

2 Answers2

4

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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
2

QByteArray::append accepts const QByteArray&, but you give it a pointer. Give it an object:

void addQByteArray(QByteArray* array) {
    p_commandArray.append(*array);
    //                    ^ dereference
}
Evg
  • 25,259
  • 5
  • 41
  • 83