Passing multiple struct arrays to class constructor

Hi everyone.

I am trying to build a class constructor that can take multiple different struct arrays, where the object modifies the original arrays, not copies. I found this example for a single array and successfully adjusted it for my project.

The trouble is, I don't really understand the syntax in the constructor, so I don't know how to add a reference to the second array (struct Signal_decode signals[]) or if it is even possible.

struct Can_frame {
    uint32_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
    uint8_t  can_dlc; /* Data Length Code (number of data bytes)*/
    uint8_t  data[8];
};

struct Signal_decode {
    uint32_t signal_frame;
    uint8_t start_byte;
    uint8_t end_byte;
    uint8_t start_bit;
    uint8_t number_of_bits;
};

Can_frame raw_frames[] =
{
    {0x5E8, 8, 0x05, 0xEF, 0x02, 0x0D, 0x06, 0x5D, 0x00, 0x8F},
    {0x5E9, 8, 0x6C, 0xDB, 0x6C, 0xDB, 0x01, 0xF5, 0x00, 0x96},
    {0x5EA, 8, 0x7A, 0xAA, 0x03, 0xE8, 0x00, 0x00, 0x6C, 0xDB},
    {0x5EB, 8, 0x05, 0xEF, 0x02, 0x0D, 0x06, 0x5D, 0x00, 0x8F},
    {0x5EC, 8, 0x05, 0xEF, 0x02, 0x0D, 0x06, 0x5D, 0x00, 0x8F}
};

struct Signal_decode signals[] =
{
    {0x5E8, 0, 1, 0, 16},
    {0x5E8, 2, 3, 0, 16},
    {0x5E8, 4, 5, 0, 16},
    {0x5E8, 6, 7, 0, 16}
};

class FrameHandler{
  public:
    template <size_t N>
    FrameHandler(Can_frame (&raw_frames)[N])
      : raw_frames(raw_frames), length(N) {}

  void printLocations() const {
    for (size_t i = 0; i < length; ++i)
      Serial.println(raw_frames[i].can_id);
  }

  private:
    Can_frame *raw_frames;
    size_t length;
};

FrameHandler canTest = raw_frames;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  canTest.printLocations();
}
void loop() {}

Any help would be greatly appreciated.

Many thanks.

The first thing I'd do is give different names to your global 'raw_frames' array, the 'raw_frames' parameter to the class constructor, and the class's 'raw_frames' private instance variable. While the compiler can keep all those straight, it's confusing for humans to look at.

Just try this:

struct Can_frame {
    uint32_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
    uint8_t  can_dlc; /* Data Length Code (number of data bytes)*/
    uint8_t  data[8];
};

struct Signal_decode {
    uint32_t signal_frame;
    uint8_t start_byte;
    uint8_t end_byte;
    uint8_t start_bit;
    uint8_t number_of_bits;
};

Can_frame raw_frames[] 
{
    {0x5E8, 8, 0x05, 0xEF, 0x02, 0x0D, 0x06, 0x5D, 0x00, 0x8F},
    {0x5E9, 8, 0x6C, 0xDB, 0x6C, 0xDB, 0x01, 0xF5, 0x00, 0x96},
    {0x5EA, 8, 0x7A, 0xAA, 0x03, 0xE8, 0x00, 0x00, 0x6C, 0xDB},
    {0x5EB, 8, 0x05, 0xEF, 0x02, 0x0D, 0x06, 0x5D, 0x00, 0x8F},
    {0x5EC, 8, 0x05, 0xEF, 0x02, 0x0D, 0x06, 0x5D, 0x00, 0x8F}
};

struct Signal_decode signals[] 
{
    {0x5E8, 0, 1, 0, 16},
    {0x5E8, 2, 3, 0, 16},
    {0x5E8, 4, 5, 0, 16},
    {0x5E8, 6, 7, 0, 16}
};

class FrameHandler{
  public:
    template <size_t RFN, size_t SN>
    FrameHandler(Can_frame (&raw_frames)[RFN], Signal_decode (&signals)[SN])
      : raw_frames{raw_frames}, rf_length{RFN}, signals{signals}, sig_length{SN} {}

  void printLocations() const {
    for (size_t i = 0; i < rf_length; ++i)
      Serial.println(raw_frames[i].can_id);
  }

  void printSignals() const {
    for (size_t i = 0; i < sig_length; ++i) {
      Serial.print(signals[i].signal_frame,HEX); Serial.print(" ");
      Serial.print(signals[i].start_byte); Serial.print(" ");
      Serial.print(signals[i].end_byte); Serial.print(" ");
      Serial.print(signals[i].start_bit); Serial.print(" ");
      Serial.println(signals[i].number_of_bits);
    }
  }

  private:
    Can_frame *raw_frames;
    size_t rf_length;
    Signal_decode *signals;
    size_t sig_length;
};

FrameHandler canTest {raw_frames, signals};

void setup() {
  Serial.begin(115200);
  while (!Serial);
  canTest.printLocations();
  canTest.printSignals();
}
void loop() {}

Thanks for your help. That works perfectly Kai-R

Then it would be nice if you mark my posting as solution :slight_smile:

Solution marked. Thanks again. :grinning:

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