Struct union : help

Hello, I was using this code in arduino-0023, and it works correctly. Now I want to use it with the arduino-ide_2.0.3_Windows_64bit ide, and I get some errors. {any solution?

        union RGB {
        struct {
            uint8_t b;
            uint8_t g;
            uint8_t r;
            //uint8_t a;
            
        } color;
        uint32_t bits;

         RGB() { bits = 0; }
         RGB(uint32_t color) { bits = color; }
         RGB(uint8_t cr, uint8_t cg, uint8_t cb) { color.r = cr; color.g = cg; color.b = cb;  }

         operator uint32_t() { return bits; }
    };
    
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

C:\Users\titi\AppData\Local\Temp\.arduinoIDE-unsaved202313-18020-xopu4w.9y7u\sketch_feb3a\sketch_feb3a.ino:31:10: error: 'RGB::RGB()' cannot be overloaded
          RGB() { bits = 0; }
          ^~~
C:\Users\titi\AppData\Local\Temp\.arduinoIDE-unsaved202313-18020-xopu4w.9y7u\sketch_feb3a\sketch_feb3a.ino:31:2: error: with 'RGB::RGB()'
          RGB() { bits = 0; }
  ^  
C:\Users\titi\AppData\Local\Temp\.arduinoIDE-unsaved202313-18020-xopu4w.9y7u\sketch_feb3a\sketch_feb3a.ino:32:10: error: 'RGB::RGB(uint32_t)' cannot be overloaded
          RGB(uint32_t color) { bits = color; }
          ^~~
C:\Users\titi\AppData\Local\Temp\.arduinoIDE-unsaved202313-18020-xopu4w.9y7u\sketch_feb3a\sketch_feb3a.ino:32:2: error: with 'RGB::RGB(uint32_t)'
          RGB(uint32_t color) { bits = color; }
  ^  
C:\Users\titi\AppData\Local\Temp\.arduinoIDE-unsaved202313-18020-xopu4w.9y7u\sketch_feb3a\sketch_feb3a.ino:33:10: error: 'RGB::RGB(uint8_t, uint8_t, uint8_t)' cannot be overloaded
          RGB(uint8_t cr, uint8_t cg, uint8_t cb) { color.r = cr; color.g = cg; color.b = cb;  }
          ^~~
C:\Users\titi\AppData\Local\Temp\.arduinoIDE-unsaved202313-18020-xopu4w.9y7u\sketch_feb3a\sketch_feb3a.ino:33:2: error: with 'RGB::RGB(uint8_t, uint8_t, uint8_t)'
          RGB(uint8_t cr, uint8_t cg, uint8_t cb) { color.r = cr; color.g = cg; color.b = cb;  }
  ^  
C:\Users\titi\AppData\Local\Temp\.arduinoIDE-unsaved202313-18020-xopu4w.9y7u\sketch_feb3a\sketch_feb3a.ino:35:10: error: 'RGB::operator uint32_t()' cannot be overloaded
          operator uint32_t() { return bits; }
          ^~~~~~~~
C:\Users\titi\AppData\Local\Temp\.arduinoIDE-unsaved202313-18020-xopu4w.9y7u\sketch_feb3a\sketch_feb3a.ino:35:2: error: with 'RGB::operator uint32_t()'
          operator uint32_t() { return bits; }
  ^       

exit status 1

Compilation error: 'RGB::RGB()' cannot be overloaded

Your code looks to me to be incomplete.

Hello. only that part gives error with ide--ide_2.0.3_Windows_64bit. It should, it should compile fine. with ide-0023 it does it correctly

I am not entirely sure what you are trying to do, but by the looks of it, you are using a union to convert between a struct and a uint32_t. If this is the case, please note that this is not allowed in C++ and may lead to some highly undesirable results. For more information, see this explanation.

may be you just want a class instead of a union

class RGB {
  private:
    uint32_t bits;
  public:
    RGB() {bits = 0;}
    RGB(uint32_t color) {bits = color;}
    RGB(uint8_t cr, uint8_t cg, uint8_t cb) {bits = ((uint32_t) cr) << 16 | ((uint32_t) cg) << 8 | ((uint32_t) cb);}
    operator uint32_t() {return bits;}
};

RGB c1;
RGB c2(0x00FF0000ul);
RGB c3(0xFF, 0x00, 0x00);

void setup() {
  Serial.begin(115200);

  Serial.print("c1 and c2 are ");
  Serial.println(c1 == c2 ? F("the same") : F("different")); // this compares the uint32_t values

  Serial.print("c2 and c3 are ");
  Serial.println(c2 == c3 ? F("the same") : F("different")); // this compares the uint32_t values
}

void loop() {}

untested but unless I made a typo you should see

c1 and c2 are different
c2 and c3 are the same

muchas gracias a todos!!

class RGB{
   public:
       union{
        
         struct {
           uint8_t b;
           uint8_t g;
           uint8_t r;
           //uint8_t a;
         }color;
         uint32_t bits;
       };
       RGB() { bits = 0; }
       RGB(uint32_t color) { bits = color; }
       RGB(uint8_t cr, uint8_t cg, uint8_t cb) { color.r = cr; color.g = cg; color.b = cb;  }


       operator uint32_t() { return bits; }
      
};

The code you posted is wrong. Please see the link posted by jfjlaros, you are not allowed to use a union like that.

This is not a good general solution but might be fine with current gcc

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.