【发布时间】:2015-12-14 13:22:17
【问题描述】:
我一直在努力解决这个问题。是不是我做错了什么,或者将 typedef 函数指针作为参数传递在 Arduino 1.6.5 中不起作用?
我有这个:
typedef int (*arithmeticOperation)(int a, int b);
还有这个:
int multiply(int a, int b){
return a*b;
}
这行得通:
arithmeticOperation result;
result = multiply;
Serial.println(result(5, 2));
如果我有这样的功能:
void printResult(int x, int y, int (*arithmeticOperation)(int, int)){
Serial.println(arithmeticOperation(x, y));
}
这个也可以
printResult(5, 3, multiply);
printResult(5, 4, result);
但是有这样的功能:
void test(int x, int y, arithmeticOperation result){
Serial.println(result(x, y));
}
这样称呼:
test(5, 4, multiply);
test(5, 4, result);
不适用于 Arduino 1.6.5。为什么?
编辑:
它不适用于 2014 年中期 MacBook Pro OSX 10.10.5 上的 Arduino IDE 1.6.5 它使用的编译器版本是 avr-g++(GCC) 4.8.1
这里是 File3.ino,它在 Arduino IDE 1.6.5 中产生了这个错误:
File3.ino:4:25: error: 'arithmeticOperation' has not been declared 'arithmeticOperation' has not been declared
答案:在 Arduino IDE 1.6.5 上不工作,但在 OSX 10.10.5 上的 Arduino IDE 1.6.6 上工作
【问题讨论】:
-
您收到编译时错误还是运行时错误?
-
你用的是什么编译器? Clang 可以很好地编译这段代码(尽管我使用的是 x64 而不是 Arduino)。
-
您是使用“C”还是“C++”(这里没有识别出“C/C++”语言)取决于您的构建调用“avr-gcc”还是“avr-g++”。
-
请发布产生错误的完整来源(不是带有错误结构被注释掉和/或省略标题的来源)和完整的未经编辑的错误消息。这意味着,从头到尾复制并粘贴它们,中间不要省略任何内容。就像,我不知道,行号。 直接在问题中发布(没有指向外部资源的链接)。
-
请取消注释您的错误行,添加任何缺少的标题,然后直接在问题中发布代码(没有外部链接)。
标签: arduino function-pointers typedef