C struct error "pointer to incomplete class type is not allowed"

11,902

You can't use NODE inside the definition of struct node, because NODE isn't defined yet.

The slow way to do it would be:

struct node {
    struct node *next;
    struct node *previous;
};

typedef struct node NODE;

so that after you define what struct node is, you can refer to it as NODE.


Change

typedef struct node{
    struct NODE *next;
    struct NODE *previous;
}NODE;

to

typedef struct node {
    struct node *next;
    struct node *previous;
} NODE;
Share:
11,902
kbick
Author by

kbick

Updated on June 12, 2022

Comments

  • kbick
    kbick almost 2 years

    I'm using Visual Studio 2013 Professional and I've tried it in Eclipse on Kali and Ubuntu as well.

    There are more areas where the same two errors occur, though I will only show some of the code here.

    I've seen a few questions related to the same problem. Most of the answers seemed to be that the struct was not previously defined though I don't think that applies here. I have also tried to putting all the code into a single source file, this changed nothing.

    Visual Studio underlines the errors in code showing error: pointer to incomplete class type is not allowed and when I build the project it shows error C2037: left of 'previous' specifies undefined struct/union 'NODE' These locations are noted in the code below.

    Another error is warning C4133: '=' : incompatible types - from 'NODE *' to 'NODE *' and the locations are noted below as well.

    So of course my question is how do I fix these errors?

    The relevant information from my header file:

    list.h
        #ifndef LIST_H
        #define LIST_H
    
        typedef struct node{
            struct NODE *next;
            struct NODE *previous;
        }NODE;
    
        typedef struct list{
            NODE node;
            int count;
        }LIST;
    
        extern void     listDelete(LIST *pList, NODE *pNode);
        extern void     listFree(LIST *pList);
        #endif
    

    The relevant information from my C source file:

    list.c
        #include "list.h"
        #define HEAD    node.next       /* first node in list */  
        #define TAIL    node.previous       /* last node in list */  
    
        void listDelete(LIST *pList, NODE *pNode)
        {
            NODE *mynode;
            if (pNode->previous == NULL)
            {
                pList->HEAD = pNode->next;
            }
            else
            {
                pNode->previous->next = pNode->next; // pointer to incomplete class type is not allowed
            }
    
            if (pNode->next == NULL)
            {
                pList->TAIL = pNode->previous;
            }
            else
            {
                pNode->next->previous = pNode->previous; // pointer to incomplete class type is not allowed
            }
    
            pList->count--;
        }
    
        void listFree(LIST *pList)
        {
            NODE *p1, *p2;
    
            if (pList->count > 0)
            {
                p1 = pList->HEAD; // warning C4133: '=' : incompatible types - from 'NODE *' to 'NODE *'
                while (p1 != NULL)
                {
                    p2 = p1->next; // warning C4133: '=' : incompatible types - from 'NODE *' to 'NODE *'
                    free((char *)p1);
                    p1 = p2;
                }
                pList->count = 0;
                pList->HEAD = pList->TAIL = NULL;
            }
        }
    
  • kbick
    kbick almost 10 years
    Yeah I see what you're saying, should have realized that.