I am stuck on this problem:
This will compile without error:
class Class1 {
public:
Class1(void) {
}
};
class Class2 {
public:
Class1 classArray[5];
Class2(void){}
};
Class2 class2;
void setup() {}
void loop() {}
but not it will not compile successfully if Class1 contains any instantiation parameters:
class Class1 {
public:
Class1(int a) { // includes instantiation parameter a
}
};
class Class2 {
public:
Class1 classArray[5];
Class2(void){}
};
Class2 class2;
void setup() {}
void loop() {}
I get the following compiler error:
/home/walt/Arduino/arduino_c/class_test/class_test.ino: In constructor 'Class2::Class2()':
class_test:11:17: error: no matching function for call to 'Class1::Class1()'
Class2(void){}
Help, please.
Try:
Class1 classArray[5]={1,2,3,4,5};
There's a warning because class one doesn't use the parameter, but it does compile.
Well, that's very interesting! But, I can work with it.
Thank you.
Discovered that I can also solve the problem by overloading the Class1 constructor:
class Class1 {
public:
Class1(void) {}
Class1(int a) {}
};
Class1 class1(1);
class Class2 {
public:
Class1 classArray[5];
Class2(void){}
};
Class2 class2;
You can indeed. I rather thought that you needed to use the parameterized constructor though.
In this case, I want to add to the array on the fly.
I'm working on a variant of Simon Monk's Timer library. I want to work with pre-defined Events and make the timer class interrupt driven..
You should probably use initializer lists:
class Class1 {
public:
Class1(uint8_t a) : x(a) { // includes instantiation parameter a
}
uint8_t x;
};
class Class2 {
public:
Class2(void) : classArray( {
1, 2, 3, 4, 5
}) {}
Class1 classArray[5];
};
Class2 class2;
void setup() {}
void loop() {}
OR:
class Class1 {
public:
Class1(uint8_t a) : x(a) { // includes instantiation parameter a
}
uint8_t x;
};
class Class2 {
public:
Class2(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e) : classArray( {
a, b, c, d, e
}) {}
Class1 classArray[5];
};
Class2 class2(1, 2, 3, 4, 5);
void setup() {}
void loop() {}
Thanks. I was not familiar with the use of initializer lists.
That is the fun of this. Even after 40 years of programming, there is still plenty to be learned/discovered.