Average calculations and arrays

Hello all

I have now tried to combine my structs into a nested struct.

/*Lamp struct*/
struct structLEDControl
{
    const byte          pin;
    const byte          sunupLowPWM;         //sun level low (morning / afternoon)
    const byte          sunupHighPWM;         //sun level high (noon)
    const byte          minPWM;         //lamp off
    const unsigned long updatePeriod;   //time /step increase (fade in/10 min)
    const unsigned long initialPauseSR; //pause on sunrise (hot pin)
    const unsigned long initialPauseSS; //pause on sunset (ambient pin)
    unsigned long       timeDelay;      //== updatePeriod + initialPause
    unsigned long       timerLED;       //== millis()
    byte                valPWM;         //
    const byte          maxPWM;
    byte                state;          //
};
/*Temperature struct*/
struct structTEMPControl
{
    const byte   pin;
    const int    maxDay;
    const int    minDay;
    const int    maxNight;
    const int    minNight;
    float        value;
    byte         valPWM;
    byte         state;
};
/*Humidity struct*/
struct structHUMControl
{
    const byte   pin;
    const int    avgDay;
    const int    minDay;
    const int    avgNight;
    float        value;
    float        total;
    long         counter;
    float        average;
};
/*Nested Struct*/
struct structMAINControl
{
    structLEDControl grLEDControl[];
    structTEMPControl grTEMPControl[];
    structHUMControl grHUMControl[];
}grMAINControl;

While defining an instance of the nested struct the compiler returned the following error message:

Arduino: 1.8.9 (Windows 10), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

'structMAINControl::structMAINControl()' is implicitly deleted because the default definition would be ill-formed:


exit status 1
use of deleted function 'structMAINControl::structMAINControl()'

Why is the struct implicitly deleted? Where lies the error in my struct definition?

Thank you

moses

The struct isn't deleted, the default constructor is. (This is the constructor that doesn't take any parameters.)
The reason is the const members of the structs.
They're const, so it doesn't make any sense to set them without initializing them, that would result in an uninitialized variable that can never be initialized.

// Mutable member
struct S {
    int i;
} s; // s.i is uninitialized
s.i = 1; // s.i is now initialized

// Immutable member
struct S {
    const int i;
} s; // s.i is uninitialized (if the constructor weren't implicitly deleted by the compiler)
s.i = 1; // illegal, because s.i is const
// s.i can never be initialized, and using it is undefined behavior

Hello PieterP

Thank you for the fast reply.

This makes sense. I dont understand however, why it works if I define and declare the individual structs by themselves without having them nested:

/*Humidity struct*/
struct structHUMControl
{
    const byte   pin;
    const int    avgDay;
    const int    minDay;
    const int    avgNight;
    float        value;
    float        total;
    long         counter;
    float        average;
};

structHUMControl grHUMControl[] =
{
    {
    // Ambient
    .pin = inputPin[DHT22HUM_AMBIENTPIN],
    .avgDay = 75,
    .minDay = 50,
    .avgNight = 90,
    .value = 0,    // mesure humidity DHT-22 ambient
    .total = 0,   // total to calculate average
    .counter = 0, // counter run total
    .average = 0
    },
    {
    // HotSpot
    .pin = inputPin[DHT22HUM_HOTPIN],
    .avgDay = 75,
    .minDay = 50,
    .avgNight = 90,
    .value = 0,    // mesure humidity DHT-22 hot
    .total = 0,   // total to calculate average
    .counter = 0, // counter run total
    .average = 0
    },
    {
    // Outside
    .pin = inputPin[DHT22OUT_PIN],
    .avgDay = 75,
    .minDay = 50,
    .avgNight = 90,
    .value = 0,    // mesure humidity DHT-22 out
    .total = 0,   // total to calculate average
    .counter = 0, // counter run total
    .average = 0
    },
};

PieterP:
I don't think I understand your question. "MAINControl" is a type, and "Main" is an instance/object of that type. They are fundamentally different things.

Compare it to integers:

int i = 42;

"int" is a type, and "i" is an instance of that type.

Bit glib that. "i" is a variable holding a value of type int. 42 is a value of type int. Variables have addresses that can be
passed around, and can change value.

Or to be fully precise, "int" denotes a type, "i" denotes a variable, "42" denotes a value... The compiler
translates all of this to runtime types, variables and values.

Technically an instance is usually an object of some class, and isn't used for simply valued types like numerics.

Terminology does change with different programming languages, I'm used to the terminology typically
employed by compiler writers, which should be pretty general.

moserroger:
Hello PieterP

Thank you for the fast reply.

This makes sense. I dont understand however, why it works if I define and declare the individual structs by themselves without having them nested:

/*Humidity struct*/

struct structHUMControl
{
    const byte  pin;
    const int    avgDay;
    const int    minDay;
    const int    avgNight;
    float        value;
    float        total;
    long        counter;
    float        average;
};

structHUMControl grHUMControl[] =
{
    {
    // Ambient
    .pin = inputPin[DHT22HUM_AMBIENTPIN],
    .avgDay = 75,
    .minDay = 50,
    .avgNight = 90,
    .value = 0,    // mesure humidity DHT-22 ambient
    .total = 0,  // total to calculate average
    .counter = 0, // counter run total
    .average = 0
    },
    {
    // HotSpot
    .pin = inputPin[DHT22HUM_HOTPIN],
    .avgDay = 75,
    .minDay = 50,
    .avgNight = 90,
    .value = 0,    // mesure humidity DHT-22 hot
    .total = 0,  // total to calculate average
    .counter = 0, // counter run total
    .average = 0
    },
    {
    // Outside
    .pin = inputPin[DHT22OUT_PIN],
    .avgDay = 75,
    .minDay = 50,
    .avgNight = 90,
    .value = 0,    // mesure humidity DHT-22 out
    .total = 0,  // total to calculate average
    .counter = 0, // counter run total
    .average = 0
    },
};

That works because you're not using the default constructor, you're using aggregate initialization.

PieterP:
That works because you're not using the default constructor, you're using aggregate initialization.

PieterP can you explain to me where this difference comes from? How to change the code that uses the default constructor to use aggregate initialization?

Is this because I dont initialize any values for the arrays?

I wanted to initialize the arrays in their individual functions because I had the impression that the code would be better readable if I dont have such a long list of variables in one piece.

Thanks.

Hello all

I tried to improve my code as follows:

struct structMAINControl
{
    structLEDControl grLEDControl[3] = { };
    structTEMPControl grTEMPControl[3] = { };
    structHUMControl grHUMControl[3] = { };
}grMAINControl;

Would that let me to initialize the instances later on? Or is it better programming practice to initialize the instances directly?

Thanks,

moses

moserroger:

struct structMAINControl

{
    structLEDControl grLEDControl[3] = { };
    structTEMPControl grTEMPControl[3] = { };
    structHUMControl grHUMControl[3] = { };
}grMAINControl;



Would that let me to initialize the instances later on? Or is it better programming practice to initialize the instances directly?

Only if there are no non-static const members in any of your structs.

It works if I use static const members?