1

学校では、議題を書かなければならず、試験、タスク、講義に関するデータを保持する必要があります

構造体の列挙型にアクセスできません。

私の構造体は次のようになります。

struct Item{

enum {TASK, EXAM, LECTURE} entryType;

char* name;
char* course;

time_t start;
time_t end;

union
{
    struct Task* task;
    struct Exam* exam;
    struct Lecture* lecture;
} typeData;
};

次に、列挙型を使用してアイテムのタイプを設定する必要があります。この構造体は Item.h で定義されています。Item.h を含む Item.c では、次のコードを使用します。

struct Item* createItem(char* type){
struct Item* newItem;

newItem = (struct Item *) malloc (sizeof(struct Item));

if (strcmp(type, "Task") == 0)
{
    //newItem->entryType = newItem->TASK;
    newItem->typeData.task = createTask();
} else if (strcmp(type, "Exam") == 0)
{
    //newItem->entryType = newItem->EXAM;
    newItem->typeData.exam = createExam();
} else if (strcmp(type, "Lecture") == 0)
{
    //newItem->entryType = newItem->LECTURE;
    newItem->typeData.lecture = createLecture();
}

return newItem;
}

コメント付きのコードでエラーが発生します(たとえば、TASKの場合):

エラー C2039: 'TASK' : 'Item' のメンバーではありません

4

2 に答える 2

3

を宣言するenumと、その内容は本質的にコンパイル時の定数になります#define。特に、 をお持ちの場合は、思っているようにではなくenum { A, B, C } foo、 でオプションにアクセスします。Afoo->A

于 2013-08-03T15:06:04.957 に答える
2

最初のポイントは不要です。2 番目に createItem のパラメーターを int に変更します。3 番目に dataType でポインターを使用しているため、これらの関数を実際に確認する必要があります。4 番目に、型と呼ばれる構造体に int のフィールドを作成します。

   struct Item* createItem(int type){
   struct Item* newItem;

   newItem = malloc (sizeof(struct Item));    

  newItem->entryType = type;

   if (type == 0)
   {
     newItem->typeData.task = createTask();
   } else if (type == 1) 
   {
     newItem->typeData.exam = createExam();
   } else if (type == 2)
   {
     newItem->typeData.lecture = createLecture();
   }

 return newItem;
 }
于 2013-08-03T15:04:53.553 に答える