2016-03-21 3 views
1

私はかなりCプログラミング(私は少し前にlazarusとjavaをやったが、市長は何もしなかった)基礎を学ぶためにRPG。 プログラムの一部にヘッダーを作成しようとして以来、コンパイル中に奇妙なエラーが発生しました。同時に私は構造体をどこにでも書くのを止め、typedefを使いました。エラーもそこから発生している可能性があります。C "typedefまたはヘッダーを使用して以来、"構造体 "エラーの前に期待される表現

advancedgame.c:

#include <stdio.h> 
#include <assert.h> 
#include <stdlib.h> 
#include <string.h> 
#include "advancedgameglobals.h" 

int main(int argc, char *argv[]) 
{ 
initiateglobals(); 
destroygameglobals(); 
return 0; 
} 

advancedgameglobals.c:

#include <stdio.h> 
#include <assert.h> 
#include <stdlib.h> 
#include <string.h> 
#include "advancedgameglobals.h" 

PLAYER *player_create(char *name, int con, int str, int agi, int wis){ 
    PLAYER *who = malloc(sizeof(PLAYER)); 
    assert(who != NULL); 

    who->name = strdup(name); 
    who->con = con; 
    who->str = str; 
    who->agi = agi; 
    who->wis = wis; 
    int def = con/5; 
    int maxhp = con * 10; 
    int hp = maxhp; 
    int maxmp = wis * 5; 
    int mp = maxmp; 
    who->def = def; 
    who->maxhp = maxhp; 
    who->hp = hp; 
    who->maxmp = maxmp; 
    who->mp = mp; 
    who->exp = 0; 
    who->level = 1; 
} 

char * determine_item_type(int type){ 
    char *typename; 
    if(type == 1){ 
    typename = "longsword";} 
    else if(type == 2){ 
    typename = "wooden shield";} 
    else{ 
    typename = "empty slot";} 
    return typename; 
} 

INVENTORY *inventory_create(char *name){ 
    INVENTORY *who = malloc(sizeof(INVENTORY)); 
    assert(who != NULL); 

    who->name = strdup(name); 
    int slot0 = 0; 
    int slot1 = 0; 
    int slot2 = 0; 
    int slot3 = 0; 
    int slot4 = 0; 
    int slot5 = 0; 
    int slot6 = 0; 
    int slot7 = 0; 
    int slot8 = 0; 
    int slot9 = 0; 

    who->slot0 = slot0; 
    who->slot0name = determine_item_type(slot0); 
    who->slot1 = slot1; 
    who->slot1name = determine_item_type(slot1); 
    who->slot2 = slot2; 
    who->slot2name = determine_item_type(slot2); 
    who->slot3 = slot3; 
    who->slot3name = determine_item_type(slot3); 
    who->slot4 = slot4; 
    who->slot4name = determine_item_type(slot4); 
    who->slot5 = slot5; 
    who->slot5name = determine_item_type(slot5); 
    who->slot6 = slot6; 
    who->slot6name = determine_item_type(slot6); 
    who->slot7 = slot7; 
    who->slot7name = determine_item_type(slot7); 
    who->slot8 = slot8; 
    who->slot8name = determine_item_type(slot8); 
    who->slot9 = slot9; 
    char *test = determine_item_type(slot9); 
    who->slot9name = test;//determine_item_type(slot9); 
} 

char * determine_town_type(int type){ 
    char *typename; 
    if(type == 1){ 
    typename = "shop";} 
    else if(type == 2){ 
    typename = "Colloseum";} 
    else{ 
    typename = "empty shack";} 
    return typename; 
} 

TOWN *town_create(char *name, int slot0, int slot1, int slot2, int slot3){ 
    TOWN *who = malloc(sizeof(TOWN)); 
    assert(who != NULL); 
    who->name = strdup(name); 
    who->slot0 = slot0; 
    who->slot0name = determine_town_type(slot0); 
    who->slot1 = slot1; 
    who->slot1name = determine_town_type(slot1); 
    who->slot2 = slot2; 
    who->slot2name = determine_town_type(slot2); 
    who->slot3 = slot3; 
    char *test = determine_town_type(slot3); 
    who->slot3name = test;//determine_town_type(slot3); 
} 

ENEMY *enemy_create(char *name, int con, int str, int agi, int wis){ 
    ENEMY *who = malloc(sizeof(ENEMY)); 
    assert(who != NULL); 

    who->name = strdup(name); 
    who->con = con; 
    who->str = str; 
    who->agi = agi; 
    who->wis = wis; 
    int def = con/5; 
    int maxhp = con * 10; 
    int hp = maxhp; 
    int maxmp = wis * 5; 
    int mp = maxmp; 
    who->def = def; 
    who->maxhp = maxhp; 
    who->hp = hp; 
    who->maxmp = maxmp; 
    who->mp = mp; 
    int exp = con+str+agi+wis; 
    who->exp = exp; 
    who->level = exp/50; 
} 

void player_destroy(PLAYER *who){ 
    assert(who != NULL); 

    free(who->name); 
    free(who); 
} 

void enemy_destroy(ENEMY *who){ 
    assert(who != NULL); 

    free(who->name); 
    free(who); 
} 

void inventory_destroy(INVENTORY *who){ 
    assert(who != NULL); 

    free(who->name); 
    free(who); 
} 

void town_destroy(TOWN *who){ 
    assert(who != NULL); 

    free(who->name); 
    free(who); 
} 

int initiateglobals(void) 
{ 
    PLAYER *PLAYER = player_create(
        "Player", 10, 10, 10, 10); 
    INVENTORY *INVENTORY = inventory_create(
         "Inventory"); 
    TOWN *TOWN = town_create(
        "Antaria", 2, 1, 0, 0); 
    return 0; 
} 

int destroygameglobals(void) 
{ 
    player_destroy(PLAYER); 
    inventory_destroy(INVENTORY); 
    town_destroy(TOWN); 
} 

advancedgameglobals.h:

#ifndef ADVANCEDGAMEGLOBALS_H 
#define ADVANCEDGAMEGLOBALS_H 

typedef struct player{ 
    char *name; 
    int con; 
    int def; 
    int str; 
    int agi; 
    int wis; 
    int maxhp; 
    int hp; 
    int maxmp; 
    int mp; 
    int exp; 
    int level; 
} PLAYER; 

typedef struct enemy{ 
    char *name; 
    int con; 
    int def; 
    int str; 
    int agi; 
    int wis; 
    int maxhp; 
    int hp; 
    int maxmp; 
    int mp; 
    int exp; 
    int level; 
} ENEMY; 

typedef struct inventory{ 
    char *name; 
    int slot0; 
    char *slot0name; 
    int slot1; 
    char *slot1name; 
    int slot2; 
    char *slot2name; 
    int slot3; 
    char *slot3name; 
    int slot4; 
    char *slot4name; 
    int slot5; 
    char *slot5name; 
    int slot6; 
    char *slot6name; 
    int slot7; 
    char *slot7name; 
    int slot8; 
    char *slot8name; 
    int slot9; 
    char *slot9name; 
} INVENTORY; 

typedef struct town{ 
    char *name; 
    int slot0; 
    char *slot0name; 
    int slot1; 
    char *slot1name; 
    int slot2; 
    char *slot2name; 
    int slot3; 
    char *slot3name; 
} TOWN; 

PLAYER *player_create(char *name, int con, int str, int agi, int wis); 
char * determine_item_type(int type); 
INVENTORY *inventory_create(char *name); 
char * determine_town_type(int type); 
TOWN *town_create(char *name, int slot0, int slot1, int slot2, int slot3); 
ENEMY *enemy_create(char *name, int con, int str, int agi, int wis); 
void player_destroy(PLAYER *who); 
void enemy_destroy(ENEMY *who); 
void inventory_destroy(INVENTORY *who); 
void town_destroy(TOWN *who); 
int initiateglobals(void); 
int destroyglobals(void); 

#endif 
ここに私のコードです

今私は取得していますエラーは私に次のように伝えます:

error: expected expression before 'PLAYER' 
player_destroy(PLAYER); 
error: expected expression before 'INVENTORY' 
inventory_destroy(INVENTORY); 
error: expected expression before 'TOWN' 
town_destroy(TOWN); 

私は私の構造のいずれかを使用して機能をしようとするたびに、私はこのエラーを取得しています。おそらくこれは非常に愚かなエラーであり、私はそれを残念に思っていますが、私はあなたが私に与えることができるどんな助けにも感謝します。

+0

これはそのエラーメッセージの原因ではないかもしれませんが、 'advancedgameglobals.c'ファイルにこれらの' extern'を入れないと思います(おそらく宣言にもないでしょう)。\ – crashmstr

+0

マクロと_enum-constants_にはすべて大文字の名前を使用します。それはC言語で唯一広く受け入れられている命名規則です。 – Olaf

+0

@crashmstr私は今それらを削除しました。ヘッダーファイルに移行するときに見つけたチュートリアルで見たので、そこにそれらを置いておきました。ありがとう。 – Plebejer

答えて

0

問題:

  • initiateglobals()はローカル変数ではなく、グローバル変数を初期化する関数。タイプと同じ名前の変数の名前付けも紛らわしいのであまり良くありません。
  • destroygameglobals()の関数呼び出しが正しくありません。引数として渡すには、型名の代わりに変数の名前を使用する必要があります。

は修正するには:

  • initiateglobals()に初期化するグローバル変数を宣言し、関数がグローバル変数ではなく、ローカル変数を初期化しています。混乱を避けるために、変数の名前は型名と異なる必要があります。
  • 関数呼び出しの型名ではなく、宣言されたグローバル変数の名前をdestroygameglobals()に使用します。
+0

助けてくれてありがとう、しかし、私はあなたがちょうど私に言ったことを実行する方法を完全に理解していません。グローバル変数は "extern"タグで初期化されます。しかし、私はtypedef(複数のストレージクラス)でそれを行うことはできませんし、 "extern PLAYER * PLAYER"も書くことはできません。私はこれが非常にひどく情報に聞こえると思うし、これをやろうとする前にもっと基礎を学ぶべきだと確信しています。しかし、私はこれを解決したら、残りの部分をより簡単に理解できるようになると思います。 – Plebejer

+0

@Plebejerここに掲載されたコードでは、「extern」タグは表示されませんが、「extern」タグで変数を初期化することは可能ですが、珍しいことです。 'extern'は*他のところで*定義されるものを*宣言するためのものです*。また、なぜ型名と同じ変数名を使用するのですか? – MikeCAT

+0

は、最初にadvancedgameglobalsのすべてがextern(構造体を除いて)とタグ付けされていました... crashmstrのコメントの後で、私はそれらを必要としないと言いました。そして、変数の名前...まあ、それが最も考えられた計画ではないと言ってみましょう。 – Plebejer

0

この機能に問題はあると思います。

int destroygameglobals(void) 
{ 
    player_destroy(PLAYER); 
    inventory_destroy(INVENTORY); 
    town_destroy(TOWN); 
} 

この関数で関数呼び出しをチェックします。いくつかの変数を引数として渡す代わりに、データ型を引数として渡しています。それは容認できません。

関連する問題