在函数C ++之间传递结构 [英] Passing struct between functions C ++
本文介绍了在函数C ++之间传递结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已搜索,但未能获得我想要的内容...
I've searched but haven't been able to get what I want...
我在做一个小游戏。我得到了包含播放器详细信息的 struct
。
I'm doing a little game. And I got this struct
that contains the player details.
struct Player
{
string name;
int level;
int exp;
int hp; // life
int mp; // mana
int shield;
};
在菜单中,用户选择开始一个新游戏,
And when in the menu, the user chooses to start a new game, it goes to this function:
int StartNewPlayer(string name)
{
Player player;
player.name = name;
player.level = 1;
player.exp = 0;
player.hp = 20;
player.mp = 5;
player.shield = 0;
*pass/return the struct here*
}
然后我有一个函数打印游戏板,我应该使用新的播放器结构的数据,例如:
Then I have a function that prints the game board, and where I should use the data from the new player struct, for example:
void game_board ()
{
cout << "Hello!" << player.name;
(...)
}
在 main
我有:
int main ()
{
StartNewPlayer(new_game());
game_board();
}
调用上述所有函数。
但我不能弄清楚...我试着参考,指针没有运气..我需要一些帮助这里请...
But I can't figure it out... I tried references, pointers without luck.. I need some help here please...
推荐答案
这是怎么回事?
Player StartNewPlayer(string name)
{
Player player;
player.name = name;
player.level = 1;
player.exp = 0;
player.hp = 20;
player.mp = 5;
player.shield = 0;
return player;
}
void game_board(Player player)
{
cout << "Hello!" << player.name;
(...)
}
int main ()
{
Player player = StartNewPlayer(new_game());
game_board(player);
}
这篇关于在函数C ++之间传递结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文