您能解释一下为什么 struct Test 不完整以及如何消除错误吗?该错误与 test.h 中的声明或 test.c 中的定义有关吗?我尝试将定义代码移动到头文件,但 createTest 不知道类型 Test,或者如果我将函数移动到头文件,则会出现错误 multipledefinition of createTest
测试.h
typedef struct STest Test;
测试.c
typedef struct STest {
int v;
char *str;
}Test;
Test *createTest(int v,char *str) {
Test *t=(Test*)malloc(sizeof(Test));
t->v=v; // error
t->str=str; // error
return t;
}
主函数(main.c)
错误: main.c|44|错误:取消引用指向不完整类型的指针
最佳答案
放置
typedef struct STest {
int v;
char *str;
} Test;
进入test.h
。
typedef struct STest Test
只是说 Test
是 struct STest
的另一个名称。目前,main.c 只知道这些。特别是,main.c
不知道该结构有哪些成员。这对我来说听起来很不完整。
关于c - 错误: dereferencing pointer to incomplete type in main file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28560648/