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?
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
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.
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.