gpt4 book ai didi

c++ - Arduino struct does not name 类型错误

转载 作者:太空狗 更新时间:2023-10-29 20:57:24 25 4
gpt4 key购买 nike

在 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;
}

这会产生 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;
}

都在 Orwell Dev-Cpp 中编译

我用的是MEGA-2560

最佳答案

您可以阅读 here关于 Arduino IDE 的构建过程。

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

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

这些定义放在文件的顶部,在您定义 Time 之前。因此在声明get_timer时,类型Time还没有声明。

克服这个问题的一种方法是将所有类型定义放在一个单独的 .h 文件中,并将其包含到您的草图中。

关于c++ - Arduino struct does not name 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30872950/

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