我在一个电机程序(我必须控制多个电机,这就是我使用结构的原因)和我的arduino MEGA一起工作.当我在驱动功能中使用它作为参数时,我不明白为什么MOTOR超出范围:
typedef struct motor
{
int EN;
/*some more ints*/
}MOTOR;
MOTOR mot1;
MOTOR mot2; /*this works with no compile error*/
int drive (MOTOR*) /*here i have compile error out of scope, neither with or without pointer*/
{
return 1;
}
void setup()
{}
void loop()
{}
sketch_jul25a:2: error: 'MOTOR' was not declared in this scope
sketch_jul25a:2: error: expected primary-expression before ')' token
sketch_jul25a.ino: In function 'int drive(MOTOR*)':
sketch_jul25a:9: error: 'int drive(MOTOR*)' redeclared as different kind of symbol
sketch_jul25a:2: error: previous declaration of 'int drive'
'MOTOR' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
因为地狱之路铺好了意图.
Arduino IDE通过在代码开头为所有用户定义的函数生成原型来尝试提供帮助.当其中一个原型引用用户定义的类型时,事情会以所描述的方式爆炸.
诀窍是让IDE无法解析代码:
namespace
{
int drive (MOTOR*)
{
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
IDE会遇到namespace
并且不知道如何处理后面的块,所以跳过它.