試圖初始化四個結構,但它說未定義。該程式在 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
我做了什么:
- 嘗試初始化一個物件
- 嘗試將問題標記到它自己的單獨檔案中,但仍然存在問題。
uj5u.com熱心網友回復:
struct在 C 中,宣告變數時必須包含關鍵字:
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..
例子
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/534173.html
標籤:C结构
