【问题标题】:C struct error "pointer to incomplete class type is not allowed"C 结构错误“不允许指向不完整类类型的指针”
【发布时间】:2014-08-16 01:50:37
【问题描述】:

我正在使用 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;
        }
    }

【问题讨论】:

  • 注意:如果使用 typedef struct struct-name {} typedef-name,typedef-name 将在全局命名空间中。 struct NODE id 不是一个有效的声明。
  • 啊好的,谢谢你的解释。

标签: c linux pointers struct


【解决方案1】:

您不能在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;

【讨论】:

  • 是的,我明白你在说什么,应该意识到这一点。
猜你喜欢
  • 1970-01-01
  • 2015-10-09
  • 2021-12-31
  • 2012-08-15
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多