0

このコードを持つC imで構造体を使用するのに問題があります。

CORRECTION NOTES 私のコードにセミコロンがあります。

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の他の部分が機能していると仮定します)未定義のエラー「ノード」があり、何が起こっているのか本当にわかりませんtypedef 構造体ノード MyStructure を構造体ノード { } 定義の下に移動しようとしても、同じエラーが発生します

ところで、tc2を使用しています

誰が私が欠けているものを指摘したいですか?

4

3 に答える 3

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);
}
于 2013-01-08T14:40:35.067 に答える
1

という名前のベア タイプを使用しようとしている場合node、それは正しくありません。そのようなタイプは存在しません。次のいずれかを使用する必要があります。

struct node my_node;

またはを使用してtypedef

MyStructure my_node;
于 2013-01-08T14:40:02.940 に答える
0

構造体の最後の } の後にセミコロンを追加する必要があります。

于 2013-01-08T14:40:15.997 に答える