错误:取消引用指向不完整类型的指针
Codeblocks 在 main.c 第 10 行 (print_bst(tree->root)) (取消引用指向不完整类型的指针)中给我这个错误,而我正在创建一个二进制搜索树,我找不到原因到这个错误。
BST.h
typedef struct Node Node;
typedef struct Tree Tree;
Tree *create_bst();
Node *create_node(int data);
void insert_bst(Tree *tree);
void print_bst(Node *root);
BST.c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
void *dataPtr;
int data;
struct Node *left;
struct Node *right;
} Node;
typedef struct Tree{
int count;
Node* root;
} Tree;
Tree *create_bst()
{
Tree *tree = (Tree*) calloc(1,sizeof(Tree));
if(tree == NULL){
printf("calloc() failed!\n");
return NULL;
}
tree->count = 0;
tree->root = NULL;
return tree;
}
Node *create_node(int data)
{
Node *node = (Node*) calloc(1, sizeof(Node));
if(node == NULL){
printf("calloc() failed!\n");
return NULL;
}
node->data = data;
node->right = NULL;
node->left = NULL;
return node;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "BST.h"
int main()
{
Tree *tree = create_bst();
while(1){
insert_bst(tree);
print_bst(tree->root);
}
return 0;
}
错误信息引用main.c中的第10行,(print_bst(tree->root))。
最佳答案
print_bst(tree->root);
是的,那是行不通的,main.c 没有 #include 任何可以告诉它 tree
有一个 root
元素的东西。
解决此问题的最简单方法是将 Tree
的定义移动到 BST.h
中,main.c
可以访问它。
关于c - 错误 : dereferencing pointer to incomplete type (Codeblocks, 在 C 中编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27806666/