简体   繁体   English

错误:在C中,在结构指针中得到错误“取消引用指向不完整类型的指针”

[英]Error: In C, got the error “dereferencing pointer to incomplete type” in a struct pointer

Hello Everybody! 大家好!

I got the following error, while trying to test a code for the game Clever Frog: error: dereferencing pointer to incomplete type 我在尝试测试游戏Clever Frog的代码时遇到以下错误: 错误:取消引用指向不完整类型的指针

The 'full code' is at pastebin.com - here (won't expire). '完整代码'位于pastebin.com - 此处 (不会过期)。 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: 我在1.c文件中定义了一个结构:

#include "1.h"
...
struct test {
   int a;
   };
...

I have a 1.h wicth have the typedef using it: 我有一个1.h wicth有typedef使用它:

...
typedef struct test testT;
...

Then I have a function that has a parameter in it depending on testT, wich is in 2.c: 然后我有一个函数,其中有一个参数取决于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 ... */
}
...

The 2.h file is like this: 2.h文件是这样的:

...
void funcTest(testT **t, int *size, ..);
...

I will pass a testT *var as the way below, at the main programam: 我将在主程序中以下面的方式传递testT * var

...
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). 奇怪的是,我在1.h文件中使用struct test时编译代码(从1.c中删除struct test - 这是错误的)。 But, when running the compiled program, exactly where the error occurs is the place of t[0]->a . 但是,当运行编译的程序时,错误发生的确切位置是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! 我已经尝试了“一切”但没有任何效果:(我相信这是非常愚蠢的事情,所以如果有人知道的话,请告诉我:D谢谢!

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). 当您试图访问a的成员t[0]结构的编译器需要知道这个结构的样子(例如,看看是否有甚至是任何a在其成员)。 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. 由于您没有将struct test类型定义放在编译器在编译2.c时可以看到的任何位置,因此会出现错误。 The compiler doesn't know what a struct test contains. 编译器不知道struct test包含什么。

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. 如果将struct test的定义放在1.h ,编译器将看到该类型的外观,并可以使用struct成员。

Just put the complete type definition in 1.h , that's where it's supposed to be. 只需将完整的类型定义放在1.h ,即它应该是的位置。

Somewhere you have a preprocessed file that has 某处你有一个预处理的文件

typedef struct test testT;

Which doesn't include 其中不包括

struct test {
   int a;
};

Preprocessing inlines all the #includes directives. 预处理内联所有#includes指令。 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. 只要你只使用一个testT指针,编译器就会知道“分配一个指针的内存值”,编译的进度会超出预期。

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. 当你真正尝试使用该指针取消引用某些东西时,编译器会意识到它需要“struct test”的完整定义,你会得到错误。

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. 如果希望结构在1.c和2.c中都可用,则必须在对两者都可见的头文件中定义它。 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. 我不知道你为什么说这是“错误的”,这是常见的做法和AFAIK没有别的办法直接解决。

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. 如果它仅在1.c中定义,则编译器在处理2.c时不知道struct test是否具有名为“a”的成员。

Another option is to just keep the forward declaration as you have now, but also include accessor/mutator functions in the header. 另一种选择是像现在一样保持前向声明,但也在头部包含accessor / mutator函数。 Then 2.c does not have to know about the "internals" of struct test , but can act on it. 然后2.c不必知道struct test的“内部”,但可以对它进行操作。 This is also very common in C APIs. 这在C API中也很常见。

(You could also define the struct identically both in 1.c and 2.c but that's a very bad idea.) (您也可以在1.c和2.c中相同地定义结构,但这是一个非常糟糕的主意。)

The definition of struct Test is only visible inside the file 1.c. struct Test的定义仅在文件1.c中可见。 The code t[0]->a doesn't see that this struct has a member named a . 代码t[0]->a没有看到这个结构有一个名为a的成员。 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. 您应该知道C / C ++分别编译每个.c文件,因此无法知道该结构是否在其他.c文件中定义。

You should perhaps do the following: 您应该执行以下操作:

(1.h) (1.H)

struct test {
    int a;
};
...

(1.c) (1.C)

#include "1.h"
...

(2.c) (2.C)

#include "1.h"
...
void funcTest(testT **t, int *size, ..){
    createMem(t,*size); /* void createMem(testT **t, int size); */
    t[0]->a = 0;
    /* ... more code ... */
}

But, when running the compiled program, exactly where the error occurs is the place of t[0]->a. 但是,当运行编译的程序时,错误发生的确切位置是t [0] - > a的位置。

The pointer to the allocated memory is actually in *t , not t (as seen from your createMatrix code at Pastebin), so you should really be doing: 指向已分配内存的指针实际上是*t ,而不是t (从createMatrix代码中可以看出),所以你应该这样做:

(*t)[0].a

and similarly in your for loop: 和你的for循环类似:

(*matriz)[i].row

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM