【发布时间】: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不是一个有效的声明。 -
啊好的,谢谢你的解释。