我在使用此代码的C im中使用结构时遇到问题。
更正错误 我的代码上有一个分号,对不起我的不好
myHeader.h
struct node{
Token elem;
void (*push)(Stack[], Token);
Token (*pop)(Stack[]);
Token (*peek)(Stack[]);
boolean (*isEmpty)(Stack[]);
boolean (*isFull)(Stack[]);
};
typedef struct node Stack;
MyMain.c
# include <stdio.h>
# include "codes/myHeader.h" <-- im using tc2 by the way so im forced to use this kind of inlcude
some codes..
当我尝试编译它时,我在MyHeader.h部分得到错误(假设.c的其他部分正在工作)它说有一个未定义的错误'node'我真的不知道什么去了on,一直在尝试移动typedef结构节点MyStructure在struct node {}定义下面仍然给出相同的错误
顺便提一下使用tc2
任何人都想指出我失踪了什么?
答案 0 :(得分:3)
typedef struct node {
int x;
int y;
} MyStructure;
同样:
struct node {
int x;
int y;
};
typedef struct node MyStructure;
堆栈实现的示例
//definitions
//C99 has #include <stdbool.h> for this
typedef short boolean;
#define true 1
#define false 0
//You may #define YOUR_APIENTRY APIENTRY (from a system header)
#define YOUR_APIENTRY
#define YOUR_APIENTRYP YOUR_APIENTRY*
//predeclarations
struct _Stack;
typedef struct _Stack Stack;
struct _StackImpl;
typedef struct _StackImpl StackImpl;
struct _Element;
typedef struct _Element Element;
//stack implementation function type definitions
typedef void (YOUR_APIENTRYP pfnPush) (Stack*, Element);
typedef Element (YOUR_APIENTRYP pfnPop) (Stack*);
typedef Element (YOUR_APIENTRYP pfnPeek) (Stack*);
typedef boolean (YOUR_APIENTRYP pfnIsEmpty) (Stack*);
typedef boolean (YOUR_APIENTRYP pfnIsFull) (Stack*);
//funct ptr table
struct _StackImpl{
pfnPush push;
pfnPop pop;
pfnPeek peek;
pfnIsEmpty isEmpty;
pfnIsFull isFull;
};
//stack
typedef struct _Stack{
Element* elems; //any appropriate container
size_t elemCount;
//if you want to replace the implementation using
//different func tables (polymorphic)
//StackImpl* funcPtrs;
} Stack;
//stack element
struct _Element{
int value;
};
//default implementation /replace NULL's with actual function pointers)
StackImpl defaultStackImpl =
{
NULL,
NULL,
NULL,
NULL,
NULL
};
//function wrappers
void push(Stack* stack, Element elem)
{
//if you use a polymorphic implementation
//stack->funcPtrs->push(stack,elem);
defaultStackImpl.push(stack,elem);
}
答案 1 :(得分:1)
如果您尝试使用名为node
的裸型,那就不对了。没有这种类型。你需要使用:
struct node my_node;
或使用typedef
:
MyStructure my_node;
答案 2 :(得分:0)
你需要在结构的最后一个之后添加一个分号。