[英]C forward declaration
(NB: it's pure C / Arduino). (注意:它是纯 C/Arduino)。
I've found tons of links my question, but no one did answer me.我在我的问题中找到了大量链接,但没有人回答我。
I have a file " Effects.h
" and " Keyboard.h
".我有一个文件“ Effects.h
”和“ Keyboard.h
”。
" Keyboard.h
" needs to include " Effects.h
". “ Keyboard.h
”需要包含“ Effects.h
”。 So " Effects.h
" can't include " Keyboard.h
"所以“ Effects.h
”不能包含“ Keyboard.h
”
in " Effects.h
" I've made those 3 functions:在“ Effects.h
”中,我做了这 3 个函数:
extern fnKeyPress effectDrops();
extern fnKeyPress effectLineRotate();
extern fnKeyPress effectRainbowCycle();
They all should return a pointer to a function that takes those two parameters: const KeyWithColor *, const struct KeyConfiguration *
它们都应该返回一个指向接受这两个参数的函数的指针: const KeyWithColor *, const struct KeyConfiguration *
So I'm trying to do this:所以我正在尝试这样做:
typedef void (*fnKeyPress)(const KeyWithColor *, const struct KeyConfiguration *);
extern fnKeyPress effectDrops();
extern fnKeyPress effectLineRotate();
extern fnKeyPress effectRainbowCycle();
But is doesn't work because it says:但它不起作用,因为它说:
Effects.h:32: error: 'KeyWithColor' does not name a type
typedef void (*fnKeyPress)(const KeyWithColor *, const struct KeyConfiguration *);
What is the workaround?解决方法是什么?
If you need to forward declare KeyWithColor and (maybe) KeyConfiguration :如果您需要转发声明 KeyWithColor 和(也许) KeyConfiguration :
(I don't know if your KeyWithColor type is a struct , i will assume that it is for simplicity) (我不知道您的 KeyWithColor 类型是否为 struct ,为了简单起见,我假设它是)
Then place this :然后放置这个:
struct KeyWithColor;
struct KeyConfiguration;
Above this :在此之上:
typedef void (*fnKeyPress)(const KeyWithColor *, const struct KeyConfiguration *);
extern fnKeyPress effectDrops();
extern fnKeyPress effectLineRotate();
extern fnKeyPress effectRainbowCycle();
Doing this you are forward declarating the types too as pointed out in the comment.这样做你也提前声明了评论中指出的类型。 And basically telling the compiler :并且基本上告诉编译器:
You don't know this yet , but i guarantee that it will be defined你还不知道,但我保证它会被定义
Be carefull to NOT put any kind of implementation in the forward declaration.注意不要在前向声明中放置任何类型的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.