'const char*' 和 'const char [2]' 类型的无效操作数转换为二进制 'operator+'

invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'

使用带有 LoRa 无线电的 Adafruit Feather M0 板,我想将 GPS 位置发送到接收器。当尝试创建一个带有 ISO 8601 时间戳和 lat/long GPS 值的数据包时,我使用以下代码创建一个 char 数组然后发送它:

char radiopacket[40] = {GPS.year + "-" + GPS.month + "-" + GPS.day + "T" + GPS.hour + ":" + GPS.minute + ":" + GPS.seconds + "Z" + "," + GPS.latitude + "," + GPS.longitude};
rf95.send((uint8_t *)radiopacket, 40);

我一直收到错误消息:

invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'

我哪里错了?

你不能像在 C 中那样连接字符串。试试像

char radiopacket[40];
sprintf(radiopacket, "%04d-%02d-%02dT%02d:%02d:%02dZ,%f,%f", GPS.year, GPS.month, GPS.day, GPS.hour, GPS.minute, GPS.seconds, GPS.latitude, GPS.longitude); 
rf95.send((uint8_t *)radiopacket, 40);

有关 sprintf

中格式字符串("%04d-...")的一些文档,请参阅 here

我猜你来自 python。

我想你需要的是 std::stringstream