错误:在C中,得到错误“解除引用指针为不完整类型”在结构指针中 [英] Error: In C, got the error "dereferencing pointer to incomplete type" in a struct pointer
问题描述
Hello Everybody!
我在尝试测试游戏Clever Frog的代码时遇到以下错误:
错误:解除引用指向不完整类型的指针
'完整代码'位于pastebin.com - 这里 (不会过期)。但我认为,有了下面的解释,任何人都可以理解。注意:我还没有实现将擦除分配的内存和其他东西的函数。
我在1.c文件中定义了一个结构:
#include1.h
...
struct test {
int a;
};
...
我有一个1.h wicth有typedef使用它:
...
typedef struct test testT;
...
然后我有一个函数,其中有一个参数取决于testT ,在2.c中:
...
void funcTest(testT ** t,int * size ,...){
/ *另一个在上面创建mem.space/alocate基于内存的输入代码的函数* /
createMem(t,* size); / * void createMem(testT ** t,int size); * /
t [0] - > a = 0; / *错误在这里* /
/ * ...更多代码... * /
}
...
2.h文件如下所示:
...
void funcTest(testT ** t,int * size,..);
...
我会传递一个 testT * var 如下所示,在主程序中:
...
testT * varTest; int size;
funcTest(& varTest,& size);
...
奇怪的是,当我使用结构测试在1.h文件(从1.c删除 struct test - 这是错误的)。但是,当运行已编译的程序时,错误发生的位置就是 t [0] - > a 的位置。
'一切'但没有任何工作:(我有信心,这是非常愚蠢的东西,所以如果有人知道的东西,请告诉我:D
谢谢!
t [0]
结构体的 a
成员时<解决方案编译器需要知道这个结构是怎么样的(例如,看看是否有任何 a
成员),因为你没有把<$ c $编译器在编译 2.c
时可以看到它的任何地方,你会得到一个错误。编译器不知道什么是 struct test
包含。 如果将 struct test
在 1.h
中,编译器会看到这个类型是怎么样的,并且可以使用这个struct成员。
只需放入完整的类型定义即可 1.h
,这就是它应该是的地方。
Hello Everybody!
I got the following error, while trying to test a code for the game Clever Frog: error: dereferencing pointer to incomplete type
The 'full code' is at pastebin.com - here (won't expire). But I think that with the explanation below, anybody can understands. Note: I haven't implemented yet the function that will erase the allocated memory and other things.
I have a struct defined in a 1.c file:
#include "1.h"
...
struct test {
int a;
};
...
I have a 1.h wicth have the typedef using it:
...
typedef struct test testT;
...
Then I have a function that has a parameter in it depending on testT, wich is in 2.c:
...
void funcTest(testT **t, int *size, ..){
/* another function that creates mem.space/alocate memory based enter code here`on the need of size above */
createMem(t,*size); /* void createMem(testT **t, int size); */
t[0]->a = 0; /*ERROR HERE*/
/* ... more code ... */
}
...
The 2.h file is like this:
...
void funcTest(testT **t, int *size, ..);
...
I will pass a testT *var as the way below, at the main programam:
...
testT *varTest; int size;
funcTest(&varTest, &size);
...
The bizarre thing is that the code compile when I use struct test at 1.h file (removing struct test from 1.c - which is wrong). But, when running the compiled program, exactly where the error occurs is the place of t[0]->a.
I already tried 'everything' but nothing worked :( I have faith that is something very stupid, so if anybody knows something, please tell me :D Thanks!
When you try to access the a
member of the t[0]
struct the compiler needs to know how this struct looks like (for example to see if there even is any a
member in it). Since you didn't put the struct test
type definition anywhere where the compiler can see it when compiling 2.c
, you get an error. The compiler doesn't know what a struct test
contains.
If you put the definition of the struct test
in 1.h
, the compiler sees how that type looks like and can use the struct members.
Just put the complete type definition in 1.h
, that's where it's supposed to be.
这篇关于错误:在C中,得到错误“解除引用指针为不完整类型”在结构指针中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!