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

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

问题描述

我在马达PROGRAMM工作(和我为什么我使用结构来控制多台电机,多数民众赞成)与我的Arduino MEGA在一起。
我dont't明白为什么MOTOR超出范围时,我用它作为驱动函数参数:

  typedef结构电机
{
   INT EN;
   / *一些整数* /
}发动机;MOTOR MOT1;
MOTOR MOT2; / *这个工程没有编译错误* /
INT驱动器(MOTOR *)/ *这里我编译出错超出范围,无论有或没有指针* /
{
   返回1;
}无效设置()
{}无效循环()
{}
sketch_jul25a:2:错误:电动机在此范围中未声明
sketch_jul25a:2:错误:之前')'标记预期主要-EX pression
sketch_jul25a.ino:在函数'诠释驱动器(MOTOR *):
sketch_jul25a:9:错误:'诠释驱动器(MOTOR *)'重新声明为不同意义的符号
sketch_jul25a:2:错误:'诠释驱动'的previous声明
电动机并不在这一范围内声明


解决方案

由于通向地狱的道路铺有良好的愿望。

Arduino的IDE试图以通过在code的开始生成所有用户定义的函数原型很有帮助。当这些原型中的一个引用一个用户定义类型,事情中所述的方式炸毁

诀窍是让code由IDE无法解析:

 命名空间
{
  INT驱动器(MOTOR *)
  {
     返回1;
  }
}

IDE将运行到命名和不知道是什么做下面的块,所以跳过它。

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.

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.

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

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

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

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

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