错误:在 C 中,出现错误“取消引用指向不完整类型的指针”在结构体指针中
大家好!
我在尝试测试“聪明青蛙”游戏的代码时遇到以下错误: 错误:取消引用指向不完整类型的指针
“完整代码”位于pastebin.com - 此处(不会过期)。但我想,通过下面的解释,任何人都可以理解。注意:我还没有实现删除分配的内存和其他东西的功能。
我在 1.c 文件中定义了一个结构:
#include "1.h"
...
struct test {
int a;
};
...
我有一个 1.h,其中 typedef 使用它:
...
typedef struct test testT;
...
然后我有一个函数,其中有一个参数取决于 testT,该参数位于 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 ... */
}
...
2. h 文件是这样的:
...
void funcTest(testT **t, int *size, ..);
...
我将在主程序中按如下方式传递 testT *var :
...
testT *varTest; int size;
funcTest(&varTest, &size);
...
奇怪的是,当我使用 struct test 时,代码会编译在 1.h 文件中(从 1.c 中删除struct test - 这是错误的)。但是,当运行编译后的程序时,错误发生的位置正是t[0]->a的位置。
我已经尝试了“一切”,但没有任何效果:(我相信这是非常愚蠢的事情,所以如果有人知道什么,请告诉我:D 谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您尝试访问
t[0]
结构的a
成员时,编译器需要知道该结构的外观(例如,查看是否存在 < code>其中的成员)。由于您没有将 struct test 类型定义放在编译器在编译 2.c 时可以看到的任何位置,因此您会收到错误。编译器不知道 struct test 包含什么。如果将
struct test
的定义放入1.h
中,编译器会看到该类型的外观并可以使用结构成员。只需将完整的类型定义放在
1.h
中即可,这就是它应该在的位置。When you try to access the
a
member of thet[0]
struct the compiler needs to know how this struct looks like (for example to see if there even is anya
member in it). Since you didn't put thestruct test
type definition anywhere where the compiler can see it when compiling2.c
, you get an error. The compiler doesn't know what astruct test
contains.If you put the definition of the
struct test
in1.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.在某个地方,您有一个预处理文件,其中
不包括
预处理内联所有#includes指令。只要您仅使用 testT 指针,编译器就会知道“分配指针的内存价值”,并且编译的进度将比预期的更进一步。
当您实际尝试使用该指针来取消引用某些内容时,编译器会意识到它需要“struct test”的完整定义,并且您会显示错误。
Somewhere you have a preprocessed file that has
Which doesn't include
Preprocessing inlines all the #includes directives. As long as you were only using a testT pointer, the compiler would have known to "allocate a pointer's worth of memory" and the compilation would have progressed further than expected.
When you actually try to use that pointer to dereference something, the compiler would then realize it NEEDED the full definition of "struct test" and you would get the error displayed.
如果您希望该结构体在 1.c 和 2.c 中都可用,则必须在两者都可见的头文件中定义它。我不知道你为什么说这是“错误的”,这是常见的做法,而且据我所知没有其他办法可以直接解决这个问题。
如果它只在 1.c 中定义,那么编译器在处理 2.c 时不知道 struct test 是否有名为“a”的成员。
另一种选择是只保留现在的前向声明,但也在标头中包含访问器/修改器函数。那么 2.c 不必了解 struct test 的“内部结构”,但可以对其进行操作。这在 C API 中也很常见。
(您也可以在 1.c 和 2.c 中相同地定义该结构,但这是一个非常坏主意。)
If you want the struct to be usable both in 1.c and 2.c, it must be defined in a header file that is visible to both. I don't know why you say that this is "wrong", it's common practice and AFAIK there is no other way around that directly.
If it's only defined in 1.c, then the compiler has no idea if
struct test
has a member named "a" when processing 2.c.Another option is to just keep the forward declaration as you have now, but also include accessor/mutator functions in the header. Then 2.c does not have to know about the "internals" of
struct test
, but can act on it. This is also very common in C APIs.(You could also define the struct identically both in 1.c and 2.c but that's a very bad idea.)
struct Test
的定义仅在文件 1.c 中可见。代码t[0]->a
没有看到该结构有一个名为a
的成员。多个编译单元之间共享的类型应该在标头中定义!您应该知道,C/C++ 单独编译每个 .c 文件,因此它无法知道该结构是在其他 .c 文件中定义的。
您也许应该执行以下操作:
(1.h)
(1.c)
(2.c)
The definition of
struct Test
is only visible inside the file 1.c. The codet[0]->a
doesn't see that this struct has a member nameda
. The types shared between several compile units shuld be defined in a header!You should know that C/C++ compiles each .c file separately, so it has no way to know that the structure is defined in some other .c file.
You should perhaps do the following:
(1.h)
(1.c)
(2.c)
指向分配内存的指针实际上位于
*t
中,而不是t
(从 Pastebin 的createMatrix
代码中可以看出),所以你应该真的正在做:在您的
for
循环中类似:The pointer to the allocated memory is actually in
*t
, nott
(as seen from yourcreateMatrix
code at Pastebin), so you should really be doing:and similarly in your
for
loop: