簡體   English   中英

結構未初始化

[英]Struct not initializing

試圖初始化四個結構,但它說未定義。 該程序在 c 中,使用 gcc 作為編譯器。

代碼如下:

struct Deck_init{
    int card1, card2;
};

// Initialize player decks
//Deck_init player1_hand, player2_hand, player3_hand, player4_hand; // Need this to work
//Deck_init player1_hand {0,0}; // Test line
//Deck_init player1_hand; // Test line

錯誤:

identifier "Deck_init" is undefined

如果需要,這里是到目前為止的代碼:

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#define NUM_THREADS 4 // Number of players 
#define NUM_CARDS_IN_DECK 52 // Cards in deck
#define PTHREADM PTHREAD_MUTEX_INITIALIZER
#define PTHREADC PTHREAD_COND_INITIALIZER


struct Deck_init{
    int card1, card2;
};

// Initialize player decks
Deck_init player1_hand, player2_hand, player3_hand, player4_hand; // Need this to work
//Deck_init player1_hand {0,0}; // Test line
//Deck_init player1_hand; // Test line

我做了什么:

  • 嘗試初始化一個 object
  • 嘗試將問題標記到它自己的單獨文件中,但仍然存在問題。

在 C 中,聲明變量時必須包含struct關鍵字:

struct Deck_Init player_hand1, player_hand2; // .. etc

或者,您可以使用typedef創建具有不同名稱的struct Deck_Init的別名。 簡單地“刪除” struct部分是很常見的,但是您可以將typedef您喜歡的任何語法上有效的名稱:

typedef struct Deck_Init{
    int card1, card;
} Deck_Init; // could just as easily be MyCoolNewDeck

...

// now you can omit the struct part
Deck_Init player_hand1; // etc..

例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM