C 结构错误"pointer to incomplete class type is not allowed"



我使用的是Visual Studio 2013 Professional,我也在Kali和Ubuntu的Eclipse中尝试过。

还有更多的地方会出现同样的两个错误,尽管我在这里只展示一些代码。

我看到了一些与相同问题相关的问题。大多数答案似乎是结构以前没有定义,但我不认为这适用于这里。我也试过把所有的代码放到一个源文件中,这没有改变什么。

Visual Studio强调代码中显示error: pointer to incomplete class type is not allowed的错误,当我构建项目时,它显示error C2037: left of 'previous' specifies undefined struct/union 'NODE',这些位置在下面的代码中注明。

另一个错误是warning C4133: '=' : incompatible types - from 'NODE *' to 'NODE *',位置也在下面。

当然我的问题是如何修复这些错误?

头文件中的相关信息:

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

我的C源文件中的相关信息:

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

不能在struct node的定义中使用NODE,因为NODE还没有定义。

比较慢的方法是:

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

,这样在你定义了struct node是什么之后,你就可以把它称为NODE


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

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

相关内容

最新更新