简体   繁体   中英

C Error: dereferencing pointer to incomplete type

So im getting a "Line 51: error: dereferencing pointer to incomplete type" when running this:

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:

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:

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)

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)

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?

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)".

Q: Which .c translation unit?

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?

Q: Why, for heaven's sake, don't you just define it in your .h file?

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

The error suggests that you have a forward declaration of Tree , but not a full definition of its corresponding structure. That is why you can declare a pointer to Tree , but you are not allowed to dereference its members.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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