Arduino - 结构超出范围为什么? [英] Arduino - struct out of scope why?

查看:32
本文介绍了Arduino - 结构超出范围为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个电机程序中工作(我必须控制多个电机,这就是我使用结构体的原因)和我的 arduino MEGA.我不明白为什么 MOTOR 在驱动函数中用作参数时超出范围:

I work at a motor programm (and i have to control multiple motors thats why i use the struct) together with my arduino MEGA. I dont't understand why MOTOR is out of scope when I use it as argument in the drive function:

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

推荐答案

因为通往地狱的道路是由善意铺就的.

Because the road to hell is paved with good intentions.

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

The Arduino IDE tries to be helpful by generating prototypes for all user-defined functions at the beginning of the code. When one of these prototypes references a user-defined type, things blow up in the manner described.

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

The trick is to make the code unparseable by the IDE:

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

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

The IDE runs into namespace and has no idea what to do with the block that follows, so skips it.

这篇关于Arduino - 结构超出范围为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆