gpt4 book ai didi

c++ struct does not name 类型错误

转载 作者:行者123 更新时间:2023-11-30 02:53:56 38 4
gpt4 key购买 nike

我在尝试编译这段代码时遇到以下错误,为什么?

“game.h:48:42: 错误:‘玩家’没有命名类型

game.h:48:50: 错误:ISO C++ 禁止声明没有类型 [-fpermissive] 的‘敌人’”

顺便说一句,第 48 行是函数“fillMap”

//game.h

#ifndef GAME_H
#define GAME_H

//CONSTANTS AND TEMPLATES

const int LEN = 40;

struct player
{
char name[LEN]; //name of character
char symbol; //character representing player on map
int posx; // x position on map
int posy; // y position on map
bool alive; // Is the player alive
double damage; // Player attacking power
double health; // Player health
};

enum direction {LEFT, RIGHT, UP, DOWN};

//PROTOTYPES

//move amount specified in position structure
void moveAmount(player& pl, int posx, int posy);

//move to specified position on map
void moveTo(player& pl, int posx, int posy);

//one player attacking other
//returns whether attack was successfull
bool attack(const player & atacker, player & target);

//one player attacking a direction on the map, epl is a pointer to an array of enemies players
//returns whether attack was sucessfull
bool attack(const player & pl, direction, player* epl);

//Check if player is dead, to be called inside attack function
inline bool isDead(const player& pl){
return pl.health <= 0 ? 1 : 0;
}

//Remove player from map if he is dead
void kill(player& pl);

//Initialize map
void fillMap(const player& player, const player* enemies, int mapWidth, int mapHeigth);

//Display map
void displayMap(const int mapWidth, const int mapHeigth);

#endif

最佳答案

问题出在这一行:

void fillMap(const player& player, const player* enemies, int mapWidth, int mapHeigth);

由于您将 player 声明为参数,因此它保留在整个前向声明的范围内。因此,它的名称隐藏了类型名称 player:在 fillMap 的前向声明中,player 指的是第一个参数,而不是 >播放器类型。

将其重命名为 pl 以解决此问题:

void fillMap(const player& pl, const player* enemies, int mapWidth, int mapHeigth);

关于c++ struct does not name 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17518131/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com