我正在尝试从我创建的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);
我该怎么办?我究竟做错了什么?
谢谢!
答案 0 :(得分:2)
struct List_t
的声明需要在头文件中。同时声明createTree
。
答案 1 :(得分:0)
您提供了三段代码,并将其标识为:
树源文件:
树头文件:
应用程序源文件:
我们称这些文件为tree.c
,tree.h
和app.c
在编译C
源文件时,通常会有一个.c
个文件,其中可能包含以下行:
#include <stdio.h>
#include "tree.h"
在里面。这就是编译器知道如何去查看另一个文件中的定义。
如果您的app.c
文件包含上述行,则app.c
中的代码可能只会使用stdio.h
和tree.h
提供的信息。
特别是,如果您在tree.c
中提供了信息,则app.c
无法看到该信息,因为没有引用它的#include
指令。
解决方案(正如其他人所说)将您的struct
定义和typedef
语句以及公共接口的任何其他部分移到{{ 1}}文件。
或者,如果您希望结构的成员是私有的,则可以提供返回数据的函数。当然,该函数的声明将是公共接口的一部分,因此它必须也在tree.h
文件中(定义函数可以在tree.h
,但声明将是公开的。