C错误 - 取消引用指向不完整类型的指针

时间:2017-12-17 14:39:16

标签: c pointers dereference

我正在尝试从我创建的Tree字段获取信息,并且我在标题中收到错误:dereferencing指向不完整类型的指针' struct List_t'

树源文件:

    struct Node_t{
Element data;
char* location;
struct Node_t*  son;
struct Node_t* next; 
};
    struct List_t{
Node head;
copyFunc copyfunc;
compareFunc compfunc;
freeFunc freefunc;
printFunc printfunc;
};

树头文件:

typedef struct Node_t* Node;
typedef struct List_t* Tree;
typedef void* Element;

应用源文件:

Tree t;
t = createTree(compareInt, copyInt , freeInt, printInt);
int* x =(int*)malloc(sizeof(int));
*x=53;
Add(t, x);
char* location;
location= t->head->location; //here I got the error
printf(location);

我该怎么办?我究竟做错了什么?

谢谢!

2 个答案:

答案 0 :(得分:2)

struct List_t的声明需要在头文件中。同时声明createTree

答案 1 :(得分:0)

您提供了三段代码,并将其标识为:

  1. 树源文件:

  2. 树头文件:

  3. 应用程序源文件:

  4. 我们称这些文件为tree.ctree.happ.c

    在编译C源文件时,通常会有一个.c个文件,其中可能包含以下行:

    #include <stdio.h>
    #include "tree.h"
    
    在里面。这就是编译器知道如何去查看另一个文件中的定义。

    如果您的app.c文件包含上述行,则app.c中的代码可能只会使用stdio.htree.h提供的信息。

    特别是,如果您在tree.c中提供了信息,则app.c无法看到该信息,因为没有引用它的#include指令。

    解决方案(正如其他人所说)将您的struct定义和typedef语句以及公共接口的任何其他部分移到{{ 1}}文件。

    或者,如果您希望结构的成员是私有的,则可以提供返回数据的函数。当然,该函数的声明将是公共接口的一部分,因此它必须也在tree.h文件中(定义函数可以在tree.h,但声明将是公开的。