简体   繁体   English

C错误:取消指向不完整类型的指针

[英]C Error: dereferencing pointer to incomplete type

So im getting a "Line 51: error: dereferencing pointer to incomplete type" when running this: 因此,在运行此代码时,我收到了“第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);
}

my addToTreeFunction: 我的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;
}

my typedefs and structs: 我的typedef和结构:

struct tADT{
int val;
};

typedef struct tADT testADT;

typedef void * TreeDataTypePtr;

Could anyone provide some insight into whats going on? 任何人都可以对发生的事情提供一些见解吗? Thanks-in advance! 提前致谢!

Edit: This is in my module (.c) 编辑:这是在我的模块(.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;
};

this is in the header (.h) 这是在标题(.h)中

struct AvlTreeNode;
struct AvlTree;

typedef struct AvlTree Tree;
typedef struct AvlNode TreeNode;
typedef void * TreeDataTypePtr;

Problem fixed by defining all structs/typedefs in header: 通过在标头中定义所有结构/类型定义来解决问题:

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;
};

Are you defining "Tree" somewhere in one of your header files? 您要在其中一个头文件中的某个位置定义“树”吗? Can line 51 of the failing module see that header? 发生故障的模块的第51行可以看到该标头吗?

You must define "Tree" :) 您必须定义“树” :)

===================== ADDENDUM ==================== =====================附录===================

Thank you for updating your post with your "definition" of Tree: 感谢您使用“树”的“定义”更新您的帖子:

// .h file
struct AvlTreeNode;
struct AvlTree;
typedef struct AvlTree Tree;
...

But the fact remains - these are both "incomplete types". 但是事实仍然存在-它们都是“不完整的类型”。

You say you defined AvlTree and AvlTreeNode "in your module (.c)". 您说您在“模块(.c)”中定义了AvlTree和AvlTreeNode。

Q: Which .c translation unit? 问: 哪个 .c翻译单位?

Q: Is it defined in that translation unit before you try to use it? 问:在尝试使用翻译单元之前 ,它是否已在该翻译单元中定义?

Q: Is it used in any other translation unit? 问:它是否在其他翻译单位中使用?

Q: Why the duplicate/redundant/confusing typedef aliases? 问:为什么重复/冗余/混淆typedef别名?

Q: Why, for heaven's sake, don't you just define it in your .h file? 问:为什么,为了天堂,您不只是在.h文件中定义它吗?

testTree->root取消引用testTree ,它是Tree类型,未在任何地方定义。

The error suggests that you have a forward declaration of Tree , but not a full definition of its corresponding structure. 该错误表明您具有Tree的前向声明,但没有其相应结构的完整定义。 That is why you can declare a pointer to Tree , but you are not allowed to dereference its members. 这就是为什么您可以声明一个指向Tree的指针,但是不允许您取消引用其成员的原因。

Make sure that the compilation unit that contains main has a #include at the top for the header file that contains the definition of struct Tree , this will fix this problem. 确保包含main的编译单元在包含struct Tree定义的头文件的顶部具有#include ,这将解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM