所以我在运行这个时得到一个“第 51 行:错误:取消引用指向不完整类型的指针”:
int main(void)
{
Tree * testTree;
testADT * data1;
testTree = createTree(compare,destroy);
data1 = malloc(sizeof(testADT));
data1->val = 10;
/* Line 51 */ addToTree(testTree,testTree->root,data1);
destroyBinTree(testTree);
return(0);
}
我的 addToTreeFunction:
TreeNode * addToTree(Tree * theTree,TreeNode * theTreeNode, TreeDataTypePtr data)
{
TreeNode * newNode;
if(isEmpty(theTree))
{
newNode = malloc(sizeof(TreeNode));
newNode->height = 0;
newNode->data = data;
theTree->root = newNode;
return theTree->root;
}else{
if(theTree->compare(theTreeNode->data,data) == 1) /* shows root data is smaller */
{
theTreeNode->right = addToTree(theTree,theTreeNode->right,data);
}else
if(theTree->compare(theTreeNode->data,data) == 0) /* shows root data is larger */
{
theTreeNode->left = addToTree(theTree,theTreeNode->left,data);
}
}
return theTreeNode;
}
我的 typedef 和结构:
struct tADT{
int val;
};
typedef struct tADT testADT;
typedef void * TreeDataTypePtr;
谁能提供一些关于发生了什么的见解?提前致谢!
编辑:这是我的模块(.c)
struct AvlNode{
void * data;
struct AvlNode * left;
struct AvlNode * right;
int height;
};
struct AvlTree{
int (*compare) (TreeDataTypePtr data1, TreeDataTypePtr data2);
void (*destroy) (TreeDataTypePtr data);
struct AvlNode * root;
};
这是在标题(.h)中
struct AvlTreeNode;
struct AvlTree;
typedef struct AvlTree Tree;
typedef struct AvlNode TreeNode;
typedef void * TreeDataTypePtr;
通过在标头中定义所有结构/类型定义来解决问题:
typedef struct AvlTree Tree;
typedef struct AvlNode TreeNode;
typedef void * TreeDataTypePtr;
struct AvlNode{
void * data;
struct AvlNode * left;
struct AvlNode * right;
int height;
};
struct AvlTree{
int (*compare) (TreeDataTypePtr data1, TreeDataTypePtr data2);
void (*destroy) (TreeDataTypePtr data);
struct AvlNode * root;
};