gpt4 book ai didi

c - Arduino - 为什么结构超出范围?

转载 作者:太空宇宙 更新时间:2023-11-04 00:47:38 24 4
gpt4 key购买 nike

我和我的 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

最佳答案

因为通往 hell 的道路是用善意铺成的。

Arduino IDE 试图通过在代码开头为所有用户定义的函数生成原型(prototype)来提供帮助。当这些原型(prototype)之一引用用户定义的类型时,事情就会以描述的方式爆炸。

诀窍是让代码无法被 IDE 解析:

namespace
{
int drive (MOTOR*)
{
return 1;
}
}

IDE 运行到 namespace 并且不知道如何处理后面的 block ,所以跳过它。

关于c - Arduino - 为什么结构超出范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31630888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com