------------------------------------- #include" lib1.h" void main(无效) { MyFunction(ts); } ------------------------------------ - lib1.h -------------------------- ----------- #include" lib2.h" void MyFunction(struct TestStructure * ts); ------------------------------------- lib1.c ---------------------------------- --- #include< stdio.h> #include" lib1.h" void MyFunction( struct TestStructure * ts) { printf("%d",ts-> a); } ------------------------------------- lib2.h ------------------------------------ - extern struct TestStructure { int a; int b; }; lib2.c ------------------------------------- #include lib2.h //没别的 ------------------------ ------------- Hi all, I can''t figure out why a can''t use my struct TestStructure in otherfiles. Any ideas? My project consists ofmain.c,lib1.h,lib1.c,lib2.c,lib2.h . main.c-------------------------------------#include "lib1.h" void main(void){MyFunction(ts);}------------------------------------- lib1.h-------------------------------------#include "lib2.h" void MyFunction(struct TestStructure *ts);------------------------------------- lib1.c-------------------------------------#include <stdio.h>#include "lib1.h" void MyFunction(struct TestStructure *ts){printf("%d",ts->a);}------------------------------------- lib2.h-------------------------------------extern struct TestStructure{int a;int b;}; lib2.c-------------------------------------#include "lib2.h"//nothing else------------------------------------- 推荐答案 Steffen Loringer< st ******* *******@freenet.de>写道: Steffen Loringer <st**************@freenet.de> wrote: 我无法弄清楚为什么不能在其他文件中使用我的struct TestStructure。有任何想法吗?我的项目包括 main.c,lib1.h,lib1.c,lib2.c,lib2.h。 main.c ------- ------------------------------ #include" lib1.h" void main(void) 咳咳!那将是主要的(无效),请。 { MyFunction(ts); } I can''t figure out why a can''t use my struct TestStructure in other files. Any ideas? My project consists of main.c,lib1.h,lib1.c,lib2.c,lib2.h . main.c ------------------------------------- #include "lib1.h" void main(void)Ahem! That''ll be int main(void), please. { MyFunction(ts); } 你永远不会定义任何名为ts的对象。你定义的函数是 _parameters_,称为ts,但是main()显然不是其中之一,而且 你从来没有在其他任何地方定义它。 Richard You never define any object called ts. You define functions taking_parameters_ called ts, but main() is (obviously) not one of those, andyou never define it anywhere else, either. Richard Steffen Loringer< st ************** @ freenet.de>写道: [...] Steffen Loringer <st**************@freenet.de> writes:[...] extern struct TestStructure { int a; int b; } ; extern struct TestStructure { int a; int b; }; [...] 这应该做什么? extern是指extern。关键字用于 对象或函数声明,而不是用于类型定义。如果您只想宣布类型,请删除extern。 - Keith Thompson(The_Other_Keith ) ks***@mib.org < http://www.ghoti.net/~kst> 圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst> 我们必须做点什么。这是事情。因此,我们必须这样做。 [...] What is this supposed to do? The "extern" keyword is used for anobject or function declaration, not for a type definition. If youjust want to declare the type, drop the "extern". --Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>We must do something. This is something. Therefore, we must do this. >我无法弄清楚为什么不能在其他>I can''t figure out why a can''t use my struct TestStructure in other文件。有任何想法吗?我的项目包含 您不能使用外部结构定义。 您可以使用具有该结构类型的外部变量,但是 你没有声明任何类型为struct TestStructure的变量, 尽管你的函数参数类型为指向结构的指针 TestStructure" ;. 未声明名为ts的main()中使用的变量,并且未初始化 。它应该是struct TestStructure *类型。 Gordon L. Burditt main.c,lib1.h,lib1.c,lib2 .c,lib2.h。 main.c ---------------------------- --------- #include" lib1.h" void main(void) {/ / MyFunction(ts); } ------------------------------------- lib1.h ------------------------------------- #include" lib2.h" void MyFunction(struct TestStructure * ts); --------------------- ---------------- lib1.c ------------------- ------------------ #include< stdio.h> #include" lib1.h" void MyFunction(struct TestStructure * ts) { printf("%d",ts-> a); } --------- ---------------------------- lib2.h ------- ------------------------------ extern struct TestStructure { int a; int b; }; lib2.c ----------------------- -------------- #include" lib2.h" //没有别的 ---------------------------- --------- files. Any ideas? My project consists ofYou cannot extern structure definitions. You can extern variables that have a type of that structure, butyou don''t declare any variables of type "struct TestStructure",although you do have function parameters of type "pointer to structTestStructure". The variable used in main() named ts is not declared andnot initialized. It should be of type struct TestStructure * . Gordon L. Burditt main.c,lib1.h,lib1.c,lib2.c,lib2.h .main.c-------------------------------------#include "lib1.h"void main(void){MyFunction(ts);}-------------------------------------lib1.h-------------------------------------#include "lib2.h"void MyFunction(struct TestStructure *ts);-------------------------------------lib1.c-------------------------------------#include <stdio.h>#include "lib1.h"void MyFunction(struct TestStructure *ts){printf("%d",ts->a);}-------------------------------------lib2.h-------------------------------------extern struct TestStructure{int a;int b;};lib2.c-------------------------------------#include "lib2.h"//nothing else------------------------------------- 这篇关于访问其他头文件中的extern结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-16 04:13