我有这段代码,我正在尝试将动态分配的数组 spiel 传递给 Guess 函数,但我从 g++ 收到以下错误
error: invalid initialization of reference of type 'Player&' from expression of type
'Players*'
Guess(*spiel, spiel->player);
我认为既然new
返回一个指针,我不应该取消引用数组??请帮助我找出我的方式的错误。
struct Game;
struct Player;
void startGame(Game *game);
void Guess(Game &game, Player & player);
bool HasPlayers(Game &game);
void PlayerStats(Game &game);
void DestroyGame(Game *game);
struct Players{
string name;
int hits;
int misses;
int guesses;
char playing;
};
struct Game{
int running;
char level;
int numPlayers;
Players *player;
};
int main()
{
Game *spiel = new struct Game();
spiel->player = new Players[spiel->numPlayers];
cout << "\nWelcome to Spiel v0.4\n\n";
startGame(spiel);
Guess(*spiel, spiel->player); <===== this line produces the error
HasPlayers(*spiel);
PlayerStats(*spiel);
DestroyGame(spiel);