自使用typedef或header

时间:2016-03-21 12:16:14

标签: c struct typedef

我对C编程很新(之前做了一点lazarus和java,但没有任何市长)我尝试编写一个小文本RPG来学习基础知识。 现在我在编译时遇到了一个奇怪的错误,因为我试图将程序的一部分作为标题。同时我停止在任何地方编写struct并使用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);

每当我尝试使用任何结构创建函数时,我都会收到此错误。很可能这是一个非常愚蠢的错误,我很抱歉,但我很感激你能给我的任何帮助。

2 个答案:

答案 0 :(得分:0)

我认为问题在于这个功能。

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

检查此功能中的函数调用。您将数据类型作为参数传递,而不是将某个变量作为参数传递。这是不可接受的。

答案 1 :(得分:0)

问题:

  • 函数entityManager初始化局部变量,而不是全局变量。将变量命名为与类型相同的名称也不好,因为它令人困惑。
  • initiateglobals()中的函数调用效果不佳。您必须使用变量名称而不是类型名称作为参数传递。

修复:

  • 声明要在destroygameglobals()中初始化的全局变量,并让函数初始化全局变量,而不是局部变量。变量名称应与类型名称不同,以避免混淆。
  • 使用声明的全局变量的名称,而不是initiateglobals()中函数调用中的类型名称作为参数。