Passing a struct to the class constructor

Hi guys :slight_smile:

There is a certain structure of arrays:

struct SENSORS{
    uint8_t pir; 
    char *loc; 
    uint8_t count;
};

SENSORS PIR[]={
     {6,"sensor1",0},
     {7,"sensor2",0},
     {8,"sensor3",0}
  };

It is necessary to somehow pass this structure to the class constructor and use the given structure inside the class.

Did this:

myclass.h

class myclass{
	public:
		myclass(struct SENSORS *PIR);
}

myclass.cpp

myclass::myclass(struct SENSORS *PIR){
	struct PIR = *PIR;
};

app.ino

#include "myclass.h"

struct SENSORS{
    uint8_t pin; 
    char *loc; 
    uint8_t count;
};

void setup() {

SENSORS PIR[]={
     {6,"sensor1",0},
     {7,"sensor2",0},
     {8,"sensor3",0}
 };

myclass test(struct SENSORS &PIR);

}


void loop() { 

}

This does not work. :frowning:

Tell me how to correctly implement this approach.

Thanks.

Something is confused. I did it like this:

/**/
//.h----------------------------
struct SENSORS{
    uint8_t pir; 
    char *loc; 
    uint8_t count;
 };

class myclass{
	public:
		SENSORS data[];
		myclass(SENSORS* pointer);
//.cpp----------------------------
myclass::myclass(SENSORS* pointer){
	data[] = pointer[];
};
//----------------------------
SENSORS PIR[]={
  {6,"sensor1",0},
  {7,"sensor2",0},
  {8,"sensor3",0}
  };

void setup() {
  Serial.begin(9600);
myclass test(&PIR[]);
Serial.println(test.data[3].pir);
}

void loop() {
}

error: flexible array member 'myclass::data' in an otherwise empty 'class myclass'

what to do? how to make it work correctly?

Please explain what you are actually trying to do.

The following code saves a pointer to the array of sensors, which might not be what you need.

Code:





```
struct Sensors {

uint8_t pir;
 const char *loc;
 uint8_t count;
};

Sensors pir[] = {
 {6, “sensor1”, 0},
 {7, “sensor2”, 0},
 {8, “sensor3”, 0}
};

class myclass{
 public:
   template <size_t N>
   myclass(Sensors color=#000000[/color][N])
     : sensorscolor=#000000[/color], lengthcolor=#000000[/color] {}

private:
   Sensors *sensors;
   size_t length;
};

myclass mySensors = pir;

void setupcolor=#000000[/color] {}
void loopcolor=#000000[/color] {}
```

|

Pieter

I need to transfer the pointer to the array to the class and use it inside the class, reading the data from the array

Then the code I posted earlier should work fine. Do you understand what it does?

No, I don’t understand. Can you explain?
Or maybe there is a simple implementation option?

maxmud:

error: flexible array member 'myclass::data' in an otherwise empty 'class myclass'

As far as I know, C++11 does not support flexible array members. This line in your code is an example of a flexible array member (flexible, because you didn’t specify a size):

SENSORS data[];

Instead, just store a pointer to one (or more) structs:

SENSORS *data;

However, there are more quirks in your code, maybe you can explain what you are trying to achieve with this class holding a pointer to a data struct array?

myclass test(&PIR[]);

This is not valid code. What address are you expecting here? Just use PIR or &PIR[0].

Serial.println(test.data[3].pir);

You have an array of size 3 (indices 0-2) and this is an out-of-bounds access.

thanks

VLAs (variable length arrays) are not allowed. If you want to save an array to a struct, you have three main options: hard-code the size, specify the size as a template parameter, or use dynamic arrays (like std::vector).

If the array doesn’t really need its own copy of the data, you can save just a pointer to an array that is stored somewhere else.
In this case, you also have to keep track of how many elements the array as.
Hence the two data members of the class: a pointer to the first element of the array, and a variable that keeps track of the length of the array:

  private:
    Sensors *sensors;
    size_t length;

To initialize the class, you’ll probably need a constructor. The constructor takes an array of N Sensors structs as a parameter. By using the “template” syntax, you tell the compiler to figure out the value of N for you. It will deduce this value from the size of the array you pass to the constructor.
You cannot pass an array by value, so you pass it by reference, indicated by the ampersand.
The array decays to a pointer, and is used to initialize the “sensors” member variable, and the number of elements N is used to initialize the “length” member variable.

  public:
    template <size_t N>
    myclass(Sensors (&sensors_array)[N])
      : sensors(sensors_array), length(N) {}

You can then use it inside of the class as follows:

[color=#00979c]struct[/color] [color=#000000]Sensors[/color] [color=#000000]{[/color]
  [color=#00979c]uint8_t[/color] [color=#000000]pir[/color][color=#000000];[/color]
  [color=#00979c]const[/color] [color=#00979c]char[/color] [color=#434f54]*[/color][color=#000000]loc[/color][color=#000000];[/color]
  [color=#00979c]uint8_t[/color] [color=#000000]count[/color][color=#000000];[/color]
[color=#000000]}[/color][color=#000000];[/color]

[color=#000000]Sensors[/color] [color=#000000]pir[/color][color=#000000][[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]{[/color]
  [color=#000000]{[/color][color=#000000]6[/color][color=#434f54],[/color] [color=#005c5f]"sensor1"[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#000000]}[/color][color=#434f54],[/color]
  [color=#000000]{[/color][color=#000000]7[/color][color=#434f54],[/color] [color=#005c5f]"sensor2"[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#000000]}[/color][color=#434f54],[/color]
  [color=#000000]{[/color][color=#000000]8[/color][color=#434f54],[/color] [color=#005c5f]"sensor3"[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#000000]}[/color]
[color=#000000]}[/color][color=#000000];[/color]

[color=#00979c]class[/color] [color=#000000]myclass[/color][color=#000000]{[/color]
  [color=#00979c]public[/color][color=#434f54]:[/color]
    [color=#5e6d03]template[/color] [color=#434f54]<[/color][b][color=#d35400]size_t[/color][/b] [color=#000000]N[/color][color=#434f54]>[/color]
    [color=#000000]myclass[/color][color=#000000]([/color][color=#000000]Sensors[/color] [color=#000000]([/color][color=#434f54]&[/color][color=#000000]sensors[/color][color=#000000])[/color][color=#000000][[/color][color=#000000]N[/color][color=#000000]][/color][color=#000000])[/color] 
      [color=#434f54]:[/color] [color=#000000]sensors[/color][color=#000000]([/color][color=#000000]sensors[/color][color=#000000])[/color][color=#434f54],[/color] [color=#d35400]length[/color][color=#000000]([/color][color=#000000]N[/color][color=#000000])[/color] [color=#000000]{[/color][color=#000000]}[/color]

  [color=#00979c]void[/color] [color=#000000]printLocations[/color][color=#000000]([/color][color=#000000])[/color] [color=#00979c]const[/color] [color=#000000]{[/color]
    [color=#5e6d03]for[/color] [color=#000000]([/color][b][color=#d35400]size_t[/color][/b] [color=#000000]i[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#000000];[/color] [color=#000000]i[/color] [color=#434f54]<[/color] [color=#d35400]length[/color][color=#000000];[/color] [color=#434f54]++[/color][color=#000000]i[/color][color=#000000])[/color]
      [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color][color=#000000]([/color][color=#000000]sensors[/color][color=#000000][[/color][color=#000000]i[/color][color=#000000]][/color][color=#434f54].[/color][color=#000000]loc[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#000000]}[/color]

  [color=#00979c]private[/color][color=#434f54]:[/color]
    [color=#000000]Sensors[/color] [color=#434f54]*[/color][color=#000000]sensors[/color][color=#000000];[/color]
    [b][color=#d35400]size_t[/color][/b] [color=#d35400]length[/color][color=#000000];[/color]
[color=#000000]}[/color][color=#000000];[/color]

[color=#000000]myclass[/color] [color=#000000]mySensors[/color] [color=#434f54]=[/color] [color=#000000]pir[/color][color=#000000];[/color]

[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000]115200[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#5e6d03]while[/color] [color=#000000]([/color][color=#434f54]![/color][b][color=#d35400]Serial[/color][/b][color=#000000])[/color][color=#000000];[/color]
  [color=#000000]mySensors[/color][color=#434f54].[/color][color=#000000]printLocations[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]
[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color][color=#000000]}[/color]

Note how the “printLocations” function iterates over all elements of the array, using the pointer and the length.