【问题标题】:passing typedef function pointers as parameter doesn't work in Arduino 1.6.5将 typedef 函数指针作为参数传递在 Arduino 1.6.5 中不起作用
【发布时间】: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

Here are some codes in gist.

这里是 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


【解决方案1】:

它不适用于 Arduino IDE 1.6.5,但它适用于 OSX 10.10.5 上的 Arduino IDE 1.6.6,两者都使用 avr-g++ (GCC) 4.8.1

【讨论】:

  • 在最新的 Fedora Linux 上使用 Arduino 1.8.13,但它不起作用。不过,您问题中的解决方法效果很好。谢谢!
猜你喜欢
  • 2019-05-11
  • 2020-11-08
  • 2021-12-18
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
相关资源
最近更新 更多