Error: no viable overloaded '='
-
Hi,
I am getting the error while assigning a QString intoan array. But, I am getting the error for x "Error: no viable overloaded '='". Why am I getting this while assigning string to an element of array?
int data = ui->lineEdit->text().toInt(); int cal = static_cast<int>(ceil(((data)*3)/2)); QString hex = QString::number( cal, 16 ).toUpper(); qDebug()<<"Calculated value:"<<hex; QString m = "0x"; QString n = hex; m.append(n); qDebug()<<"append:"<<m; sendPacket[0]=m;
-
@Mohit-Tripathi
What array? Is the error onsendPacket[0]=m;
, why not tell us? Assuming it is, how can we answer if we don't know the declaration ofsendPacket
? -
Hi,
@JonB Yes, the error is on sendPacket[0]=m. I have declared in my code but still i am getting the error.
Declaration is " QString sendPacket".Thanks
-
@Mohit-Tripathi
If your actual declaration isQString sendPacket
then it is not an array (of
QString
s, I mean), is it, so why do you say it is an array or index it like an array? You tell me what typesendPacket[0]
is? -
@Mohit-Tripathi You are trying to assign a QString to a char: https://doc.qt.io/qt-5/qbytearray.html#operator-5b-5d-1
This can't work... -
@Mohit-Tripathi said in Error: no viable overloaded '=':
@JonB
Hi,
It is QByteArray sendPacket; not QString sendPacket.And there's the issue isn't it, QByteArray != QString
What exactly do you want? A "List" of QStrings, or do you want the content of your QString as a QByteArray?
-
@J-Hilk
Hi,
I want to store the 16 bit value in a string. I am able to store the 8 bit but not 16 bit. I am able to this:
QByteArray ba;
ba.resize(5);
ba[0] = 0x3c;But, not like this:
QByteArray ba;
ba.resize(5);
ba[0] = 0x60c1;This is exactly what I am trying to do. After that, I am able to transmit the 8 bit data but not 16 bit.
void transmit(QSerialPort & port, const QByteArray & data) { port.write(data); port.flush(); qDebug() << "\nWrote" << data.size() << ":" << data.toHex().constData(); chkError(port); }
-
@Mohit-Tripathi that is correct,
Like @jsulm said, QByteArray only accepts char as it lowest form.
You have to assign the 1st index the high byte and the 2nd index (of the byte array) the Low byte (or flipped, depending on endianness )
That said, your previous int to hex string conversion is unneeded and probably wrong too.