【问题标题】:Arduino servo struct "does not name a type"Arduino伺服结构“没有命名类型”
【发布时间】:2012-11-13 08:06:48
【问题描述】:

我正在尝试运行一个使用包含伺服对象的结构的 arduino 程序,它给了我这个错误:

error: 'leg' does not name a type

我认为我在内存管理方面做错了,但我对此相当陌生,因此感谢任何帮助。

这是我的代码:

#include <Servo.h> 

typedef struct{
 Servo hip;
 Servo shin;
 Servo foot;
}leg;



int currentPin = 0; //this is the pin that the leg will be attached to


leg getLeg(void){
  leg newLeg;
  newLeg.hip.attach(currentPin++);
  newLeg.shin.attach(currentPin++);
  newLeg.foot.attach(currentPin++);

  return newLeg;  

}

void setup() 
{ 
  leg frontLeft = getLeg();
  leg frontRight = getLeg();
  leg backRight = getLeg();
  leg backLeft = getLeg();
} 


void loop() 
{ 


} 

【问题讨论】:

  • “struct_Name”出现在哪里?
  • 我试图概括错误,现在应该更清楚了。

标签: c memory-management struct arduino


【解决方案1】:

试试这个

struct legtype {
    Servo hip;
    Servo shin;
    Servo foot;
};

typedef legtype leg;

这行得通吗?

干杯,

【讨论】:

  • 您已经用“C”标记了这个问题 - 真的是这样还​​是正在编译为 C++?无论如何,为什么typedefstruct 而不是仅仅使用struct leg newLeg;
  • 是的,它是 C,我只知道 C,到目前为止我还不错。除非.. 会是 C++ 吗?
  • 好的,我已经使用 struct leg 修复了它。感谢您的帮助
  • 是的...看看这个 SO 线程解释了 C 和 C++ 中 typedef 的复杂性:stackoverflow.com/questions/612328/…
【解决方案2】:

在头文件中定义结构为我解决了这个问题。 在 arduino 中创建一个名为:“whatever.h”的新选项卡 在whatever.h中定义结构 在主文件中包含whatever.h。

【讨论】:

  • 唯一对我有用的选项。但我不明白为什么 struct-typedef 必须在单独的头文件中。顺便说一句,我用 Arduino 做 ESP8266。
  • 感谢您的信息!我也不明白。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多