简体   繁体   中英

conflict data type quint16_be from QtEndian with stdint.h

I want to use qint16_be, quint16_be, quint32_be etc. as a data type for big endian what I am trying is something like this

#include <QtEndian>
typedef quint16_be uint16_t;

And I am getting error

C:/Qt/5.15.2/mingw81_64/include/QtCore/qendian.h:429:28: error: conflicting declaration 'typedef QBEInteger<short int> qint16_be'
 typedef QBEInteger<qint16> qint16_be;
 C:/Qt/Tools/mingw810_64/x86_64-w64-mingw32/include/stdint.h:38:25: note: previous declaration as 'typedef short unsigned int qint16_be'
 typedef unsigned short  qint16_be;

I have not included stdint.h in my project. Any idea how would I resolve it

Here we go: an MCVE for qToBigEndian() :

#include <QtCore>

int main()
{
  const uint16_t sample = 0x0201;
  QByteArray data((const char*)&sample, sizeof sample);
  qDebug() << data;
  const uint16_t sampleBE = qToBigEndian(sample);
  QByteArray dataBE((const char*)&sampleBE, sizeof sampleBE);
  qDebug() << dataBE;
}

Output:

"\x01\x02"
"\x02\x01"

I ran this sample in VS2019 on my Intel laptop which has little-endian architecture.
Respectively, the byte order was changed due to the call of qToBigEndian() .

The conversion to QByteArray was a quick hack.
Nevertheless, conversion of const uint16_t* to const char* is allowed (with the intention to get access to the byte representation of something).

memcpy() is another permitted way to achieve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM