[英]Error: dereferencing pointer to incomplete type (Codeblocks, Programming in C)
錯誤:取消引用不完整類型的指針
當我創建二進制搜索樹時,代碼塊在main.c第10行(print_bst(tree-> root))(向不完整類型的指針引用)中給了我這個錯誤,但我找不到導致此錯誤的原因。
BST小時
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
#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
可以訪問它。
主文件中包含的頭文件
#include "BST.h"
不包含結構節點和樹的定義。 它只聲明它們
typedef struct Node Node;
typedef struct Tree Tree;
因此在這個陳述中
print_bst(tree->root);
編譯器將發出錯誤,因為它不知道結構樹是否具有數據成員根。
而且似乎數據成員void *dataPtr
可能已從結構Node中刪除,因為未使用它。 對於臨時對象,您可以在函數中聲明局部變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.