Go Down

Topic: How to create an array of objects [SOLVED] (Read 6185 times) previous topic - next topic

jbellavance

Aug 09, 2017, 12:06 am Last Edit: Aug 09, 2017, 01:19 am by jbellavance
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:
Code: [Select]
//===============================================================================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:
Code: [Select]
Click MYKeypad[];
MYkeypad[0](2, PULLUP);

would do the trick.  :smiley-sad:
I get:
Code: [Select]
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:
Code: [Select]
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.

Code: [Select]
Click MYkeypad[0] = new Click(2, PULLUP);
This compiled OK
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Delta_G

#1
Aug 09, 2017, 12:21 am Last Edit: Aug 09, 2017, 12:22 am by Delta_G
Code: [Select]
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:

Code: [Select]
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,

Code: [Select]
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. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

jbellavance

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

Still looking on the net.

Jacques

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Delta_G

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. 

Code: [Select]
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);
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Delta_G

Code: [Select]
Click MYkeypad[] = {Click(2, PULLUP), Click(3, PULLUP), Click(4, PULLUP), Click(5, PULLUP)};

Maybe it has to be that way.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

jbellavance

#5
Aug 09, 2017, 01:05 am Last Edit: Aug 09, 2017, 01:10 am by jbellavance
I think I am getting closer.

This compiles:
Code: [Select]
#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
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jbellavance

Horray!

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

Thanks for your help!

Jacques
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up