Initialize const array in class object

Hello,
I´m currently controlling 25 stepper motors with the v1 adafruit shield. I had to change the library to use more than one shield. Controlling works already but I´m stuck to initialize an array timepositions for each stepper object. That’s what the class structure looks like:

class AF_Stepper {
 public:
  AF_Stepper();
  void init(uint16_t, uint8_t);
.
.
.
  uint8_t timepositions[];
 private:
  uint8_t current step;

AF_Stepper::AF_Stepper() {
}

void AF_Stepper::init(uint16_t steps, uint8_t num) {
  revsteps = steps;
  steppernum = num;
  currentstep = 0;
  switch (steppernumm) {
    case 2:
       uint8_t timepos[] = { 5, 25, 35, 55};
      break;
    case 3:
      uint8_t timepos[] = {2, 10, 50};
      break;
.
.
.
    case 23:
      uint8_t timepos[] = {1, 6};
      break;
  }
  for(unsigned int i=0; i < sizeof(timepos)/sizeof(uint8_t); i++) {
    timepositions[i]=timepos[i];
  }

Depending on the stepper number different constant positions should be useable in a way like:
motor[1].timepositions[1]
Whats the correct way to implement those constants?

Probably none, at least like you want to have it.

The arrays have different lengths, but an object has to have a fixed length.

In your code you made the array zero elements long, for all steppers. Allocating arrays on the stack with the same name and throwing them away does not help. The case and the for could be removed by the compiler.

Does you code compile? I certainly does not work.

You could give all steppers an array of the maximum length and a field that denotes how many are used.

You have two choices:

1) Define the length of the array in the 'h file. Set the length to the MAXIMUM number of elements the array will ever hold.

2) Instead of an array, declare a pointer, and use dynamic allocation (i.e. - malloc()) to reserve the correct amount of memory for the array when your init function is called.

Regards, Ray L.

Thanks for your reply. I declare now a pointer and use dynamic allocation:

 uint8_t Size;
  switch (steppernumm) {
    case 2:
      Size = 4;
      uint8_t timepos[4] = { 5, 25, 35, 55};
      break;
    case 3:
      Size = 2;
      uint8_t timepos[2] = { 10, 50};
      break;
.
.
.
    case 23:
      Size = 1;
      uint8_t timepos[1] = { 6};
      break;
  }
  timepositions = malloc(Size*sizeof(uint8_t))
  for (unsigned int i = 0; i < Size; i++) {
    timepositions[i] = timepos[i];
  }

Problem is I still don’t know how to fill the dynamic array in a nice way. This way my code doesn’t compile

AFMotor.cpp:153:23: error: redeclaration of 'uint8_t times [5]'

That variable isn't mentioned in your code, must be an unrelated matter. You have 'timepos' and 'timepositions', not 'times'.

Your switch won't compile, anything declared inside any case label all share the same scope and would be your redefinition error if you changed 'timepos' to 'times'.

To get a better idea of what you’re after, tell me a few things:

Do you use a single instance of the class, or one for each stepper. As in do you call init() to change the internals multiple times, or is it called only once per instance somewhere at the start of the sketch?

If there is many instances of the class, do they need to be in an array (it seems like it from your example ‘motor[1].timepositions[1]’)?