【发布时间】:2021-03-10 10:20:22
【问题描述】:
我需要能够为一个类指定一个函数(回调函数?)作为菜单系统的一部分,我的 C++ 知识在这里延伸。显然这不会编译,但希望它能让我了解我正在尝试做什么 -
void testFunc(byte option) {
Serial.print("Hello the option is: ");
Serial.println(option);
}
typedef void (*GeneralFunction)(byte para);
GeneralFunction p_testFunc = testFunc;
class testClass {
GeneralFunction *functionName;
public:
void doFunction() {
functionName;
}
};
testClass test { *p_testFunc(123) };
void setup() {
Serial.begin(9600);
test.doFunction();
}
void loop() {
}
我知道一些 std:: 选项,但遗憾的是 Arduino 没有实现它们。
编辑:此代码的编译器输出 -
sketch_mar10a:17:29: error: void value not ignored as it ought to be
testClass test { *p_testFunc(123) };
^
sketch_mar10a:17:35: error: no matching function for call to 'testClass::testClass(<brace-enclosed initializer list>)'
testClass test { *p_testFunc(123) };
^
【问题讨论】:
-
没有什么是很明显的。请在问题中包含编译器错误消息
标签: c++ class pointers arduino-c++