【问题标题】:C "expected expression before 'struct'" error since using typedef or header由于使用 typedef 或标头,C“'struct'之前的预期表达式”错误
【发布时间】:2016-03-21 12:16:13
【问题描述】:

我对 C 编程相当陌生(之前做过一点 lazarus 和 java,但什么都没有),我尝试编写一些 text-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);

每当我尝试使用我的任何结构创建函数时都会收到此错误。很可能这是一个非常愚蠢的错误,对此我深表歉意,但如果您能给我任何帮助,我将不胜感激。

【问题讨论】:

  • 这可能不是该错误消息的原因,但我认为您不希望在您的 advancedgameglobals.c 文件中包含那些 externs(并且可能也不在声明中)。\
  • 只对宏和枚举常量使用全大写的名字。这是 C 语言中唯一被广泛接受的命名约定。
  • @crashmstr 我现在已经删除了它们,只是将它们放在那里,因为我在转换到头文件时发现的教程中看到了它们。谢谢。
  • @Olaf 我也在一个据称可靠的“教程”中找到了一些东西,我会改变它,谢谢。
  • 有正确答案已被删除。问题是像PLAYER *PLAYER = player_create("Player", 10, 10, 10, 10); 这样的行。在 C 中,当您定义 typedef 时,它的行为就像一个 new 关键字。不能使用同名变量。在 C++ 中你可以。

标签: c struct typedef


【解决方案1】:

问题:

  • 函数initiateglobals() 初始化局部变量,而不是全局变量。将变量命名为与类型相同的名称也不好,因为它会造成混淆。
  • destroygameglobals() 中的函数调用不好。您必须使用变量名称而不是类型名称作为参数传递。

修复:

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

【讨论】:

  • 感谢您的帮助,但我不完全明白如何执行您刚刚告诉我的内容。全局变量是用“extern”标签初始化的,对吧?但是我不能用 typedef(多个存储类)来做到这一点,我也不能写“extern PLAYER *PLAYER”。我确信这听起来非常糟糕,在尝试这样做之前我应该​​更多地学习基础知识。但我认为,一旦我看到了解决方案,我将能够更轻松地理解其余部分。
  • @Plebejer 我在此处发布的代码中看不到任何“extern”标签,但使用“extern”标签初始化变量是不寻常的,尽管它是允许的。 extern 用于声明什么将被在别处定义。还有,你为什么愿意使用与类型名相同的变量名?
  • 一开始,advancedgameglobals 中的所有内容都被标记为 extern(显然结构除外)...在 crashmstr 告诉我我不需要它们的评论后删除了它。还有变量名……好吧,让我们说这不是最深思熟虑的计划。
  • 我做到了! ^^ 谢谢你的帮助。我对“外部”有一个错误的概念,但现在我明白了。重命名了大多数变量(例如 player_struct)并在 advancedgameglobals.h 中的那些 typedef 声明后面键入 player_struct* player_root(例如)并使用这些变量。
【解决方案2】:

我认为这个功能有问题。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2023-03-07
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 2015-03-31
    • 2012-07-08
    相关资源
    最近更新 更多