How to create an array of objects [SOLVED]

Hi all,

I have been successfull at creating all the classes I wanted in a .h file.

In the sketch, I want to create an array of this:

//===============================================================================Click
//Constructor: (Inherits Contact)
//  pin: the Arduino pin on which the switch attached
//  mode: either PULLUP or PULLDOWN depending on the wiring
//clicked(): Returns true if the switch was closed.
//           Waits for the switch to be re-opened before returning
//-------------------------------------------------------------------
class Click : public Contact {
  public:
    Click(byte pin, byte mode);
    virtual bool clicked();
};//Click------------------------------------------------------------

I thought that:

Click MYKeypad[];
MYkeypad[0](2, PULLUP);

would do the trick. :slightly_frowning_face:
I get:

no matching function for call to 'Click::Click()'

The goal would be to treat the switches with a for( ; ; ) construct.

Thanks for the help.

Jacques

EDIT:
Just tried:

Click MYkeypad[0] = new Click(2, PULLUP);
Click MYkeypad[1] = new Click(3, PULLUP);
Click MYkeypad[2] = new Click(4, PULLUP);
Click MYkeypad[3] = new Click(5, PULLUP);

It looks as MYkeypad is not treated as an array.

Click MYkeypad[0] = new Click(2, PULLUP);

This compiled OK

Click MYKeypad[];

This line creates an array of 0 objects. That's not very useful. But it does cause the class constructor to be called. And you don't have an option for an empty constructor. And you haven't given it any parameters to pass to the one you do have.

You can:

Click MYKeypad[] = {(2, PULLUP), (3, PULLUP), (4, PULLUP)};

Or you can create the array and fill it later, but if you do that then you have to give it a size.

Right here,

MYkeypad[0](2, PULLUP);

You are talking about MYKeypad[0] which doesn't exist in your 0 size array.

And even so, that's not the right way to call the class constructor.

1 Like

Hi, I triedClick MYkeypad[] = {(2, PULLUP), (3, PULLUP), (4, PULLUP), (5, PULLUP)};I get :conversion from 'int' to non-scalar type 'Click' requestedfor the four items in the list.

Still looking on the net.

Jacques

Again, in relation to your edit, you are working on objects that don't exist. I you don't provide a brace enclosed initializer then you MUST give the array a size. Otherwise you are going to clobber memory that you don't own with that array.

Click MYkeypad[0] = new Click(2, PULLUP);
Click MYkeypad[1] = new Click(3, PULLUP);
Click MYkeypad[2] = new Click(4, PULLUP);
Click MYkeypad[3] = new Click(5, PULLUP);
Click MYkeypad[] = {Click(2, PULLUP), Click(3, PULLUP), Click(4, PULLUP), Click(5, PULLUP)};

Maybe it has to be that way.

I think I am getting closer.

This compiles:

#include "ClassTest.h"
Click *MYkeypad[4];

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  MYkeypad[0] = (2, PULLUP);
  MYkeypad[1] = (3, PULLUP);
  MYkeypad[2] = (4, PULLUP);
  MYkeypad[3] = (5, PULLUP);
}

byte readKeypad() {
  for (int i = 0 ; i < 4 ; i++) {
    if (MYkeypad[i]->clicked()) return i;
  }
  return 99;
}
void loop(){
  byte key = readKeypad();
  if (key != 99) Serial.println(key);
}

strangely, pin 13 flashes, and nothings happens on the monitor

Jacques

Horray!

TheClick MYkeypad[] = {Click(2, PULLUP), Click(3, PULLUP), Click(4, PULLUP), Click(5, PULLUP)};Works like a charm!

Thanks for your help!

Jacques

1 Like