Struct compiles for some boards not for others "could not convert"

this will not compile using the Tools M5StickC setting. Works fine compiling for esp32 or 8266. any ideas why I get this error? And how do I get around this and create an array for variables as in the struct?

struct sHue {
  int  usedOnFloor;
  char deviceNum[3]    = {0};
  char state[6]        = {0};
  char sat[4]          = {0};
  char bri[4]          = {0};
  char x[5]            = {0};
  char y[5]            = {0};
  char transtime[5]    = {0};
  char descrip[28]     = {0};
  char productName[28] = {0};
  char givenName[28]   = {0};
};

sHue HueDevice[] = {
  {0, "0",  "this ", "one", "is ", "not ", "used", "10",  "Not Registered",        "0", "0"},
  {1, "1",  "false", "254", "200", "4598", "4106", "10",  "Behind table",       "0", "0"},
  {1, "2",  "false", "254", "200", "4598", "4106", "10",  "Under Rail Picture",    "0", "0"}, 
  {1, "10", "false", "254", "200", "4598", "4106", "10",  "Ceiling Can Pinball",   "0", "0"},
  {3, "11", "false", "254", "200", "4598", "4106", "99",  "Master BR Corner",      "0", "0"},
  {2, "12", "false", "254", "200", "4598", "4106", "10",  "NW Corner",             "0", "0"},
  {1, "13", "false", "254", "200", "4598", "4106", "10",  "Chairside TV Center",   "0", "0"},
  {2, "14", "false", "254", "200", "4598", "4106", "10",  "NE Corner",             "0", "0"},
  {2, "15", "false", "254", "200", "4598", "4106", "10",  "SE Corner",             "0", "0"}, 
  {1, "29", "false", "254", "200", "4598", "4106", "10",  "reserved",              "0", "0"},
  {9, "99",     " ",   " ",   " ",    " ",    " ",  " ",  "loop stopper",          "0", "0"}
};```

x.h:47:1: error: could not convert '{0, "0", "this ", "one", "is ", "not ", "used", "10", "Not Registered", "0", "0"}' from '' to 'sHue'
};
^```

Please show the code that produced this error

The final line in your HueDevice[] initialization list should NOT have a comma after it.
That said, I have no experience with the board you indicated. But putting the comma after the end of a line tells the compiler more are coming.

  • Wes

Actually it doesn't, scroll down.

I did, that's all you need to produce the error, that and set the board to M5StickC

My mistake, I needed to scroll SIDEWAYS to see the actual line ends. That said, I have no other suggestions, lacking the board in my "boards" section.

  • Wes

It's an interesting little gadget m5stick c plus. Like an esp32 with display imu sensor and more.

It doesn't, or I don't see it. But in truth it would be OK. C/C++ explicitly allows for that odd wrong-looking last comma for reasons.

You could google it if your curiosity won't be contained just knowing but not knowing why.

a7

The error occurs on the AVR boards (I tried it on the UNO), but not the ESP8266, megaAVR, or minicore board platforms.

struct sHue {
  int  usedOnFloor;
  char deviceNum[3]    = {0};
  char state[6]        = {0};
  char sat[4]          = {0};
  char bri[4]          = {0};
  char x[5]            = {0};
  char y[5]            = {0};
  char transtime[5]    = {0};
  char descrip[28]     = {0};
  char productName[28] = {0};
  char givenName[28]   = {0};
};

sHue HueDevice =  {0, "0",  "this ", "one", "is ", "not ", "used", "10",  "Not Registered",        "0", "0"};

void setup(){}
void loop(){}
BlinkWithoutDelay:15:108: error: could not convert '{0, "0", "this ", "one", "is ", "not ", "used", "10", "Not Registered", "0", "0"}' from '<brace-enclosed initializer list>' to 'sHue'
 sHue HueDevice =  {0, "0",  "this ", "one", "is ", "not ", "used", "10",  "Not Registered",        "0", "0"};
                                                                                                            ^
exit status 1
could not convert '{0, "0", "this ", "one", "is ", "not ", "used", "10", "Not Registered", "0", "0"}' from '<brace-enclosed initializer list>' to 'sHue'

< edit >
There is no error if the default values are removed from the struct.

WAG is there a different math library required for your board?

can you post compiler output in full for both boards?

Try to remove the = {0} everywhere.

Sure that gets rid of the error, but the question was why the code compiles without errors or warnings on sone board platforms while others show an error.

It's strange to hear such a question from you :slight_smile:
As i think this is a result of using different toolchains, different inlude files and defines, possibly different versions of the compiler...

I did notice the AVR platform uses gcc++11 while minicore uses gcc++17. I'm assuming some change in the compiler or c++ standards, but have not found any examples like the OPs, most struct initialization and default value examples do not involve elements that are arrays.

I think I found it:

I think you are on to the answer, so it works on gcc++17 just nothing else then. In playing around with this this morning I notice it works with 8266 but not esp32. I previously thought it worked with the latter.

Oh, and not to pretend I know what any of this is, I looked in the compiler messages for the esp8266 and see stuff like -std=gnu++17 and Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed

I also checked against Uno and Uno Wifi Rev 2 and they don't compile either.

Ok, so what is the easiest way to initialize this table?

In a constructor? With an initializer list?

Ok, well that's what it's called I guess, I will look into it. But now I wonder how I ever knew to do it this way with esp8266. I learn by seeing what others do making what that is work for what I want to do. Thanks.

Don't you just remove the default initialisation (all the {0} initialisers) and continue using your explicit initialisation lists when you create sHue objects ?