C ++与函数联合,Arduino编译器错误“无法重载”

时间:2019-05-08 13:35:35

标签: c++ compiler-errors arduino unions

我正在为Arduino写一个库,现在只能将字节序错误的字节数组转换为浮点数uint32 oruint16。

我已经获得了工作代码,其中我使用memcpy将以正确的字节序反转的数组复制到float的内存中,但是现在我发现union { uint8_t bytes[4]; float result; }是一种快速而有效的方法但使用更少的资源。

我提出的概念可以在我的PC上运行(使用cygwin在Windows 10上为C ++ 14),但是由于'float Converter::ToFloat(uint8_t*)' cannot be overloaded我无法在Arduino IDE上为我的Arduino进行编译,因此我不知道编译器在哪里得到我超载的想法。

union Converter {
  float ToFloat(uint8_t data[]) {
    inValue[0] = data[3];
    inValue[1] = data[2];
    inValue[2] = data[1];
    inValue[3] = data[0];
    return floatOutValue;
  };
  uint32_t ToUint32(uint8_t data[]) {
    inValue[0] = data[3];
    inValue[1] = data[2];
    inValue[2] = data[1];
    inValue[3] = data[0];
    return uint32OutValue;
  };
  uint32_t ToUint16(uint8_t data[]) {
    inValue[0] = data[1];
    inValue[1] = data[0];
    return uint16OutValue;
  };
private:
  uint8_t inValue[4];
  float floatOutValue;
  uint32_t uint32OutValue;
  uint16_t uint16OutValue;
};

void setup() {
  uint8_t t1[4] = {0x0A, 0xC3, 0x53, 0x48}; //180573000
  Converter converter;
  Serial.println(converter.ToUint32(t1));
  uint8_t t2[2] = {0x07, 0xE3}; //2019
  Serial.println(converter.ToUint16(t2));
  uint8_t t3 = 0x05; //5
  Serial.println(t3 * 1);
  uint8_t t4[9][4] = {
    {0x3F, 0x7F, 0xCC, 0x8C}, //0.99921
    {0x3D, 0x21, 0x43, 0x00}, //0.039371
    {0x3B, 0x92, 0x07, 0xDF}, //0.0044565
    {0xBD, 0x21, 0xBE, 0x6E}, //-0.039488
    {0x3F, 0x7F, 0xAE, 0x7E}, //0.99876
    {0x3C, 0xF9, 0x64, 0x38}, //0.030443
    {0xBB, 0x55, 0x26, 0x18}, //-0.0032524
    {0xBC, 0xFA, 0xA3, 0x24}, //-0.030595
    {0x3F, 0x7F, 0xE0, 0xFC}, //0.99953
  };
  for (int i = 0; i < 9; i++) {
    Serial.println(converter.ToFloat(t4[i]));
  }
}

那么,为什么这段代码不能为Arduino编译,却可以在Windows 10上以正确的输出完美运行? 编译器错误的真正含义是什么?

1 个答案:

答案 0 :(得分:0)

解决方案:如果它在arduino上不起作用,请不要尝试做。

我发现,如果删除联合中的所有函数(其中一个函数除外),则如果该函数具有参数列表(uint8_t data []),则它将编译,但是当我向参数列表中添加任何第二个参数时,它将不编译,并且我不知道为什么。

我遇到的许多不同问题似乎都没有记载,因此我将放弃工会职能,而采取一种简单但不太优雅的解决方案,将职能置于工会之外,并将工会成员定义为公开,像这样:

static union {
    float floatValue;
    uint32_t uint32Value;
    uint16_t uint16Value;
    uint8_t inValue[4];
};

float bytesToFloat(uint8_t data[]){
    inValue[ 0 ] = data[ 3 ];
    inValue[ 1 ] = data[ 2 ];
    inValue[ 2 ] = data[ 1 ];
    inValue[ 3 ] = data[ 0 ];
    return floatValue;
}
uint32_t bytesToUint32(uint8_t data[]){
    inValue[ 0 ] = data[ 3 ];
    inValue[ 1 ] = data[ 2 ];
    inValue[ 2 ] = data[ 1 ];
    inValue[ 3 ] = data[ 0 ];
    return uint32Value;
}
uint16_t bytesToUint16(uint8_t data[]){
    inValue[ 0 ] = data[ 1 ];
    inValue[ 1 ] = data[ 0 ];
    return uint16Value;
}

void setup() {
    uint8_t t1[4] = {0x0A, 0xC3, 0x53, 0x48}; //180573000
    cout << bytesToUint32(t1) << endl;
    uint8_t t2[2] = {0x07, 0xE3}; //2019
    cout << bytesToUint16(t2) << endl;
    uint8_t t3 = 0x05; //5
    cout << t3 * 1 << endl;
    cout.precision(5);
    uint8_t t4[9][4] = {
            {0x3F, 0x7F, 0xCC, 0x8C}, //0.99921
            {0x3D, 0x21, 0x43, 0x00}, //0.039371
            {0x3B, 0x92, 0x07, 0xDF}, //0.0044565
            {0xBD, 0x21, 0xBE, 0x6E}, //-0.039488
            {0x3F, 0x7F, 0xAE, 0x7E}, //0.99876
            {0x3C, 0xF9, 0x64, 0x38}, //0.030443
            {0xBB, 0x55, 0x26, 0x18}, //-0.0032524
            {0xBC, 0xFA, 0xA3, 0x24}, //-0.030595
            {0x3F, 0x7F, 0xE0, 0xFC}, //0.99953
    };
    for (int i = 0; i < 9; i++) {
        cout << bytesToFloat(t4[ i ]) << endl;
    }
}