kbi*_*ick 2 c linux struct pointers
我正在使用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
Run Code Online (Sandbox Code Playgroud)
我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
你不能NODE在里面定义使用struct node,因为NODE还没有定义.
缓慢的做法是:
struct node {
struct node *next;
struct node *previous;
};
typedef struct node NODE;
Run Code Online (Sandbox Code Playgroud)
因此,在定义了什么之后struct node,您可以将其称为NODE.
更改
typedef struct node{
struct NODE *next;
struct NODE *previous;
}NODE;
Run Code Online (Sandbox Code Playgroud)
至
typedef struct node {
struct node *next;
struct node *previous;
} NODE;
Run Code Online (Sandbox Code Playgroud)