簡體   English   中英

c ++ struct沒有命名類型錯誤

[英]c++ struct does not name a type error

嘗試編譯此代碼時出現以下錯誤,為什么?

“game.h:48:42:錯誤:'玩家'沒有命名類型

game.h:48:50:錯誤:ISO C ++禁止聲明'敵人'沒有類型[-fpermissive]“

第48行是功能“fillMap”btw

//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指的是第一個參數,而不是player類型。

將其重命名為pl以解決此問題:

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

因為player不是一個類型; struct player是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM