Arduino结构未命名类型错误

use*_*963 3 c++ arduino

在Arduino IDE中,我可以创建具有自定义类型的变量,但不能从函数返回自定义类型:

这样编译

struct Timer
{
  Timer()
  {
  }
};

Timer t;
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}
int main()
{
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这会产生Timer does not name a type错误:

struct Timer
{
  Timer()
  {
  }
};

Timer get_timer()
{
  return Timer();
}

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}
int main()
{
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

两者都在Orwell Dev-Cpp中编译

我用MEGA-2560

Art*_*ium 5

您可以在此处阅读有关Arduino IDE的构建过程。

在将其编译之前,需要将您的草图转换为有效的C ++文件。

这种转换的一部分是为所有函数声明创建函数定义。

这些定义位于文件的顶部,位于的定义之前Time。因此,在声明时get_timerTime尚未声明类型。

解决此问题的一种方法是将所有类型定义放在单独的.h文件中,并将其包含在草图中。