构造函数 class 需要参数但想将其用作 属性
Constructor class requires parameters but want to use it as a property
所以我有一个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);
}
使用 arduino 键盘库@https://playground.arduino.cc/Code/Keypad/
但是它是 posting 错误(在 post 的底部)因为(我相信)Keypad 构造函数需要参数。有什么技巧可以让我的 class 工作或使用其他模式来防止这个问题吗?
感谢您提供的任何帮助。我对这一切都很陌生。
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
用构造函数替换init
,Keyblock(byte * keypadRowPins, byte * keypadColPins, Debug &debug)
。
这不是“模式”,而是默认选择。
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.
}
Is there a trick to making this work
不是技巧,但由于 Keypad
没有默认构造函数,您需要在 Keyblock
构造函数的 member initializer list 中初始化它。
示例:
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)
{}
所以我有一个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);
}
使用 arduino 键盘库@https://playground.arduino.cc/Code/Keypad/
但是它是 posting 错误(在 post 的底部)因为(我相信)Keypad 构造函数需要参数。有什么技巧可以让我的 class 工作或使用其他模式来防止这个问题吗?
感谢您提供的任何帮助。我对这一切都很陌生。
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
用构造函数替换init
,Keyblock(byte * keypadRowPins, byte * keypadColPins, Debug &debug)
。
这不是“模式”,而是默认选择。
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.
}
Is there a trick to making this work
不是技巧,但由于 Keypad
没有默认构造函数,您需要在 Keyblock
构造函数的 member initializer list 中初始化它。
示例:
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)
{}