0

So I have a class

#include <SPI.h>
#include <Keypad.h>
#include "Debug.h"

class Keyblock {  
  private:
    Debug debug;
    Keypad keypad;
    
  public:
    void init(byte * keypadRowPins, byte * keypadColPins, Debug &debug);
    void read();
};


void Keyblock::init(byte * keypadRowPins, byte * keypadColPins, Debug &debug) {
  this->debug = debug;
  char keys[4][3] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
  };
  keypad = Keypad(makeKeymap(keys), keypadRowPins, keypadColPins, 4, 3);
}

void Keyblock::read() {
  char key = keypad.getKey();
  debug.message(key);
}

that uses the Arduino keypad library @ https://playground.arduino.cc/Code/Keypad/

However it is posting errors (at bottom of post) because (I believe) the Keypad constructor requires parameters. Is there a trick to making this work or another pattern for my class that will prevent this issue?

I am very new at all of this.

In file included from sketch\Box.h:7:0,
                 from D:\source\Main\Main.ino:2:
sketch\Keyblock.h:8:7: note: 'Keyblock::Keyblock()' is implicitly deleted because the default definition would be ill-formed:
 class Keyblock {
       ^~~~~~~~
Keyblock.h:8:7: error: no matching function for call to 'Keypad::Keypad()'
In file included from sketch\Keyblock.h:5:0,
                 from sketch\Box.h:7,
                 from D:\source\Main\Main.ino:2:
C:\Users\campo\Documents\Arduino\libraries\Keypad\src/Keypad.h:78:2: note: candidate: Keypad::Keypad(char*, byte*, byte*, byte, byte)
  Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols);
  ^~~~~~
C:\Users\----\Documents\Arduino\libraries\Keypad\src/Keypad.h:78:2: note:   candidate expects 5 arguments, 0 provided
halfer
  • 19,824
  • 17
  • 99
  • 186
hendr1x
  • 1,470
  • 1
  • 14
  • 23
  • You don't show how you create objects. But `Keypad` has no default constructor, and you leave it to be default constructed in your class. – sweenish Aug 25 '21 at 19:43
  • You need to define a constructor for your class, and the constructor should specify the initialization of the `keypad` member by calling `Keypad()` with appropriate arguments. – Barmar Aug 25 '21 at 19:44
  • 1
    Change your `init` function into a constructor, and then use [this](https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) to intialize all of the members of `Keyblock` – NathanOliver Aug 25 '21 at 19:46
  • Thank you for your help. I tried as hard as I could to get your method to work and could not. If you are available I started a new question with specifics @ https://stackoverflow.com/questions/68957036/keypad-library-wont-work-in-member-initialization-list – hendr1x Aug 27 '21 at 17:02
  • 1
    @TedLyngmo : Thanks for your help. I appreciate it. My other question got shut down but I am going to work on it today with the comments provided. – hendr1x Aug 28 '21 at 13:35
  • @hendr1x Great! You're welcome! – Ted Lyngmo Aug 28 '21 at 14:13

2 Answers2

1

Replace init with a constructor, Keyblock(byte * keypadRowPins, byte * keypadColPins, Debug &debug).
This is not a "pattern" but the default choice.

char keys[4][3] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
};



Keyblock::Keyblock(byte * keypadRowPins, byte * keypadColPins, Debug &debug)
    : debug(debug),
      keypad(makeKeymap(keys), keypadRowPins, keypadColPins, 4, 3)
{
    // This space intentionally left blank.
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thank you for your help. I tried as hard as I could to get your method to work and could not. If you are available I started a new question with specifics @ https://stackoverflow.com/questions/68957036/keypad-library-wont-work-in-member-initialization-list – hendr1x Aug 27 '21 at 17:02
1

Is there a trick to making this work

Not a trick, but since Keypad doesn't have a default constructor, you need to initialize it in the member initializer list of your Keyblock constructor(s).

Example:

class Keyblock {  
private:
    static constexpr byte ROWS = 4;
    static constexpr byte COLS = 3;

    char keys[ROWS][COLS] = {
      {'1','2','3'},
      {'4','5','6'},
      {'7','8','9'},
      {'*','0','#'}
    };

    // put your pins in these arrays:
    byte keypadRowPins[ROWS] = { 9, 8, 7, 6 };
    byte keypadColPins[COLS] = { 12, 11, 10 };

    Keypad keypad;

public:
    Keyblock(); // add a user-defined default constructor
};

Keyblock::Keyblock() : // member init-list between `:` and the body of the constructor
    keypad(makeKeymap(keys), keypadRowPins, keypadColPins, ROWS, COLS)
{}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Thank you for your help. I tried as hard as I could to get your method to work and could not. If you are available I started a new question with specifics @ https://stackoverflow.com/questions/68957036/keypad-library-wont-work-in-member-initialization-list – hendr1x Aug 27 '21 at 17:02
  • @hendr1x I assume the problem you had was that `Keypad` doesn't accept `const` arguments and must be allowed to make changes to the arguments (which seems odd, but it's an old library). I updated the answer to work with non-`const` arguments. – Ted Lyngmo Aug 29 '21 at 08:41