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

标签 c linux pointers struct

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

同样的两个错误发生的地方还有很多,不过我这里只展示部分代码。

我见过几个与同一问题相关的问题。大多数答案似乎是该结构以前未定义,但我认为这不适用于此处。我还尝试将所有代码放入一个源文件中,这没有任何改变。

Visual Studio 强调代码中的错误显示 error: pointer to incomplete class type is not allowed 当我构建项目时它显示 error C2037: left of 'previous' specified 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;

关于C 结构错误 "pointer to incomplete class type is not allowed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25336248/

相关文章:

c - C中用于获取字符串的指针算法

c - 是什么原因导致 libzmq.dll 崩溃?

C 基于用户输入的每行字符缩进

c++ - 为什么c++ pthread会自行退出而不导致进程崩溃?

linux - 在 apache access_log 文件中发现错误

c++ - 从模板参数指向方法的指针

c - 为什么打包不能跨同级 union 或结构工作

C sendto 有时可以工作...只是在一段时间后停止...没有错误

linux - Linux与libmraa vs rtos

c - [错误]请求其中是指针类型的成员,也许您打算在 C BST 中使用 -> ?)