私はかなり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);
私は私の構造のいずれかを使用して機能をしようとするたびに、私はこのエラーを取得しています。おそらくこれは非常に愚かなエラーであり、私はそれを残念に思っていますが、私はあなたが私に与えることができるどんな助けにも感謝します。
これはそのエラーメッセージの原因ではないかもしれませんが、 'advancedgameglobals.c'ファイルにこれらの' extern'を入れないと思います(おそらく宣言にもないでしょう)。\ – crashmstr
マクロと_enum-constants_にはすべて大文字の名前を使用します。それはC言語で唯一広く受け入れられている命名規則です。 – Olaf
@crashmstr私は今それらを削除しました。ヘッダーファイルに移行するときに見つけたチュートリアルで見たので、そこにそれらを置いておきました。ありがとう。 – Plebejer