gpt4 book ai didi

c - Typedef 不作为参数工作或在 Arduino 草图函数中返回

转载 作者:太空狗 更新时间:2023-10-29 15:29:11 27 4
gpt4 key购买 nike

我正在尝试在 Arduino ide 中创建一个链表。

代码如下:

typedef struct ll{  
struct ll * prev;
unsigned int currTime;
unsigned int stopTime;
struct ll * next;
} TIMING;

TIMING * head;
TIMING * current;

void setup();
void add_new_first(int stopTimer, TIMING * head);

void main() {
init();
setup();
// Code that uses the def
head->prev = NULL;
head->next = NULL;
head->currTime = 0;
head->stopTime = 200;

}

最后还有函数的实际代码。

这个例子给我错误:

service_timer_cpp.cpp:5:34: error: ‘TIMING’ has not been declared.

当不使用定义的类型作为参数或从函数返回时,它可以正常工作。

这就是我认为它可以在 c 中完成的方式,并且早些时候对我有用。我不明白为什么它不能在 Arduino 环境中工作。

最佳答案

根据 one of our fellow Adruino developers ,预处理器的工作方式与常规 C 不同。编译期间会即时生成一些代码,这在 http://arduino.cc/en/Hacking/BuildProcess 中有详细说明。 :

Transformations to the main sketch file

The Arduino environment performs a few transformations to your main sketch file (the concatenation of all the tabs in the sketch without extensions) before passing it to the avr-gcc compiler. First, #include "Arduino.h", or for versions less than 1.0, #include "WProgram.h" is added to the top of your sketch. This header file (found in /hardware/cores//) includes all the defintions needed for the standard Arduino core.

Next, the environment searches for function definitions within your main sketch file and creates declarations (prototypes) for them. These are inserted after any comments or pre-processor statements (#includes or #defines), but before any other statements (including type declarations). This means that if you want to use a custom type as a function argument, you should declare it within a separate header file. Also, this generation isn't perfect: it won't create prototypes for functions that have default argument values, or which are declared within a namespace or class.

Finally, the contents of the current target's main.cxx file are appended to the bottom of your sketch.

这就是为什么 struct ll 可以作为类型正常工作(不需要 typedef),但是 typedef'ing 会导致代码中出现您看不到的编译错误。

关于c - Typedef 不作为参数工作或在 Arduino 草图函数中返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24252631/

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