Structure initialization (error: non-trivial initialization)

I'm getting an error compiling the code snippet below:

C:...sketch_timerTest1.ino:32:1: sorry, unimplemented: non-trivial designated initializers not supported
};
^

exit status 1

Compilation error: exit status 1'

struct portBit
{
  uint16_t addr;
  uint8_t bit;
};
struct mdConst
{
  portBit pwmOut;
  portBit limCW;
  portBit limCCW;
  portBit dirOut;
  portBit enaOut;
  portBit brkInp;
  uint16_t timrBase;
};

struct mdVar
{
  uint16_t rpm;
  uint16_t accel;
  float moveLen;
  float stepLen;
  bool enabled;
  bool dirEqCW;
};


mdConst mdConstTbl =
{  // Timer 3
  .pwmOut = {PORTE, 4},
  .enaOut = {PORTF, 3}
};

void setup() {
  // put your setup code here, to run once:

}

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

}

I believe this kind of structure initialization works with the GNU compiler (assuming I got the {} placements correct) although I haven't actually tried it there (yet) and I don't know what exactly the Arduino compiler doesn't like.

The overall goal here is to use the same subroutines to control 3 identical timers and associated sets of discrete I/O using arrays of structures containing the pertinent constant and variable data.

-lance

Hello lancekfisher

Take a search engine of your choice and ask the WWW for 'sorry, unimplemented: non-trivial designated initializers not supported'.

HTH

If you solve the warnings warning: missing initializer for member 'mdConst::someName' [-Wmissing-field-initializers], the error disdappears :wink:

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_839048\sketch_apr16a.ino:32:1: sorry, unimplemented: non-trivial designated initializers not supported

 };

 ^

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_839048\sketch_apr16a.ino:32:1: warning: missing initializer for member 'mdConst::limCCW' [-Wmissing-field-initializers]

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_839048\sketch_apr16a.ino:32:1: warning: missing initializer for member 'mdConst::dirOut' [-Wmissing-field-initializers]

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_839048\sketch_apr16a.ino:32:1: warning: missing initializer for member 'mdConst::enaOut' [-Wmissing-field-initializers]

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_839048\sketch_apr16a.ino:32:1: warning: missing initializer for member 'mdConst::brkInp' [-Wmissing-field-initializers]

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_839048\sketch_apr16a.ino:32:1: warning: missing initializer for member 'mdConst::timrBase' [-Wmissing-field-initializers]

exit status 1
Error compiling for board Arduino Mega or Mega 2560.

If you don't see the warnings, set "Compiler warnings" to "ALL" in File → Preferences.

mdConst mdConstTbl {
    {PORTE, 4},
    {0,     0},
    {0,     0},
    {0,     0},
    {PORTF, 3},
    {0,     0},
    0
};

you could add a constructor and define what you want to initialize.

struct portBit
{
  uint16_t addr;
  uint8_t bit;
};
struct mdConst
{
  portBit pwmOut;
  portBit limCW;
  portBit limCCW;
  portBit dirOut;
  portBit enaOut;
  portBit brkInp;
  uint16_t timrBase;

  mdConst(portBit pwmOut, portBit enaOut) : pwmOut(pwmOut), enaOut(enaOut){}
};

struct mdVar
{
  uint16_t rpm;
  uint16_t accel;
  float moveLen;
  float stepLen;
  bool enabled;
  bool dirEqCW;
};

/*
// ok with initialzer list
mdConst mdConstTbl =
{  // Timer 3
  {PORTE, 4},
  {PORTF, 3}
};
*/

mdConst mdConstTbl =
{  // Timer 3
  .pwmOut = {PORTE, 4},
  .enaOut = {PORTF, 3}
};

/*
// when no initializer
mdConst mdConstTbl {
    {PORTE, 4},
    {0,     0},
    {0,     0},
    {0,     0},
    {PORTF, 3},
    {0,     0},
    0
};
*/

void setup() {
  Serial.begin(115200);
  Serial.println(mdConstTbl.pwmOut.bit);

}

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

}

Or you can do this:

struct portBit {
  uint16_t addr;
  uint8_t bit;
};

struct mdConst {
  portBit pwmOut;
  portBit limCW;
  portBit limCCW;
  portBit dirOut;
  portBit enaOut;
  portBit brkInp;
  uint16_t timrBase;
};

struct mdVar {
  uint16_t rpm;
  uint16_t accel;
  float moveLen;
  float stepLen;
  bool enabled;
  bool dirEqCW;
};

mdConst mdConstTbl;

void setup() {
  Serial.begin(115200);
  mdConstTbl.pwmOut = {PORTE, 4};
  mdConstTbl.enaOut = {PORTF, 3};

  Serial.println(mdConstTbl.pwmOut.bit);
}

void loop() {}

Now you can pick out what you like best.

Thanks for the help. I had tried searching for the error text on the forum but not on the whole web thinking this was an IDE issue. I now see that C++ doesn't support what I was trying to do since constructors are supposed to be used for that. For the past 10 years I've been using C rather than C++ for my embedded projects and have become a bit unknowledgeable WRT C++ constructs and aren't familiar enough to be confident I can make that work, especially since I plan to force the table of constants into flash.

Putting the initialization elements in the same order with no gaps does appear to work though so I've got two solutions now.

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