在 Arduino 中使用虚拟方法
Using virtual methods with Arduino
我有一小段 Arduino 代码给我编译错误:
error: no matching function for call to 'PushButton::PushButton(int, LeftButtonEvent*)
在它自己的头文件中我有两个 classes:
class Event
{
public:
virtual void handle() {
}
};
class PushButton
{
public:
PushButton(int pinButton, Event *event);
uint8_t read();
private:
uint8_t _buttonState;
Event _event;
};
以及 classes 的源文件:
PushButton::PushButton(int pinButton, Event *event)
{
// implementation
}
uint8_t PushButton::read() {
// implementation
return _buttonState;
}
在主要的 ino sketch 头文件中,我定义了一个新的 class 扩展事件 class:
class LeftButtonEvent : public Event {
public:
virtual void handle();
};
并且在草图源文件中我提供了 handle 方法的实现并使用它:
void LeftButtonEvent::handle() {
log("Is working!!!!!");
}
LeftButtonEvent leftButtonEvent;
PushButton leftButton;
void setup() {
leftButton = PushButton(PIN_LEFT_BUTTON, &leftButtonEvent);
}
我原以为 PushButton 的构造函数会接受 LeftButtonEvent 类型,因为它扩展了 Event class,但它看起来并不喜欢它。
我错过了什么吗?
由于只有不完整的代码,我无法直接测试它,所以有一个例子说明如何让它工作(都在一个草图中,Arduino IDE 1.6.12,C++ 11):
class Event {
public:
virtual void handle() = 0;
};
class EventLeft : public Event {
public:
virtual void handle() {
Serial.println("EventLeft");
}
} leftEvent;
class EventRight : public Event {
public:
virtual void handle() {
Serial.println("EventRight");
}
} rightEvent;
class PushButton {
public:
PushButton(int8_t _pin, Event * _event) : pin(_pin), state(true), event(_event) {
pinMode(pin, INPUT_PULLUP);
}
void check() {
if (! digitalRead(pin)) { // inverted logic
if (state) event->handle();
state = false;
} else {
state = true;
}
}
private:
int8_t pin;
bool state;
Event * event;
};
PushButton buttons[] = {
{4, &leftEvent},
{5, &rightEvent}
};
void setup() {
Serial.begin(115200);
}
void loop() {
delay(10);
for (PushButton & button : buttons) button.check();
//// if the range based for loop above doesn't work, you have to use old school one:
// for (uint8_t i = 0; i < 2; ++i) buttons[i].check();
}
我有一小段 Arduino 代码给我编译错误:
error: no matching function for call to 'PushButton::PushButton(int, LeftButtonEvent*)
在它自己的头文件中我有两个 classes:
class Event
{
public:
virtual void handle() {
}
};
class PushButton
{
public:
PushButton(int pinButton, Event *event);
uint8_t read();
private:
uint8_t _buttonState;
Event _event;
};
以及 classes 的源文件:
PushButton::PushButton(int pinButton, Event *event)
{
// implementation
}
uint8_t PushButton::read() {
// implementation
return _buttonState;
}
在主要的 ino sketch 头文件中,我定义了一个新的 class 扩展事件 class:
class LeftButtonEvent : public Event {
public:
virtual void handle();
};
并且在草图源文件中我提供了 handle 方法的实现并使用它:
void LeftButtonEvent::handle() {
log("Is working!!!!!");
}
LeftButtonEvent leftButtonEvent;
PushButton leftButton;
void setup() {
leftButton = PushButton(PIN_LEFT_BUTTON, &leftButtonEvent);
}
我原以为 PushButton 的构造函数会接受 LeftButtonEvent 类型,因为它扩展了 Event class,但它看起来并不喜欢它。 我错过了什么吗?
由于只有不完整的代码,我无法直接测试它,所以有一个例子说明如何让它工作(都在一个草图中,Arduino IDE 1.6.12,C++ 11):
class Event {
public:
virtual void handle() = 0;
};
class EventLeft : public Event {
public:
virtual void handle() {
Serial.println("EventLeft");
}
} leftEvent;
class EventRight : public Event {
public:
virtual void handle() {
Serial.println("EventRight");
}
} rightEvent;
class PushButton {
public:
PushButton(int8_t _pin, Event * _event) : pin(_pin), state(true), event(_event) {
pinMode(pin, INPUT_PULLUP);
}
void check() {
if (! digitalRead(pin)) { // inverted logic
if (state) event->handle();
state = false;
} else {
state = true;
}
}
private:
int8_t pin;
bool state;
Event * event;
};
PushButton buttons[] = {
{4, &leftEvent},
{5, &rightEvent}
};
void setup() {
Serial.begin(115200);
}
void loop() {
delay(10);
for (PushButton & button : buttons) button.check();
//// if the range based for loop above doesn't work, you have to use old school one:
// for (uint8_t i = 0; i < 2; ++i) buttons[i].check();
}