Ahm*_*ihi 1 c pointers compiler-errors codeblocks binary-search-tree
错误:取消引用指向不完整类型的指针
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);
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
错误消息引用main.c中的第10行(print_bst(tree-> root)).
print_bst(tree->root);
Run Code Online (Sandbox Code Playgroud)
是的,那不会起作用,main.c不会#include任何可以告诉它tree有root元素的东西.
解决这个问题的最简单的方法是移动的定义Tree为BST.h在那里main.c可以访问它.
| 归档时间: |
|
| 查看次数: |
585 次 |
| 最近记录: |