I've been trying to make an array of ezButtons, but my CPP days are very far behind.
This is how I tried :
ezButton *Buttons = new ezButton[5]; // create an array of ezButton objects
Buttons[0] = ezButton(2); // create ezButton object that attach to pin 2
Buttons[1] = ezButton(3); // create ezButton object that attach to pin 3
Buttons[2] = ezButton(4); // create ezButton object that attach to pin 4
Buttons[3] = ezButton(5); // create ezButton object that attach to pin 5
Buttons[4] = ezButton(6); // create ezButton object that attach to pin 6
But I get an error : "no matching function for call to 'ezButton::ezButton()'
Apparently, there is no suitable constructor for such a call in the ezButton.cpp file.
Is this possible ?
Should I modify the ezButton library and add a constructor ? (and how ? As I said, my cpp is VERY rusty, like 15 years old.)
Or should I just copy my code 5 times, which I find very stupid and time consuming, which isn't the goal of programming to begin with...
That's what I noticed, and initially thought I'd modify the ezButton class to add a default constructor, but the solution I've been proposed was so much simpler that I realised my idea was pointless.
Unlike in other languages like Java, you should almost never use the new keyword in C++.
If you need a fixed-size array, simply create it on the stack as shown in the answer posted by Whandall.
If you need a dynamically allocated array, use an std::vector, which allows you to create array of types without a default constructor (with all the caveats of dynamic allocation on microcontrollers, of course).