C++ c++;用另一个类的指针设置一个类的值
我试图实现游戏交易或不交易,我有两个类,方块和球员。盒子里储存的是矢量游戏盒,我想在玩家里面储存盒子和钱,玩家必须保留到游戏结束 我曾尝试以这种方式实施它。它运行正确,没有错误,但是当我尝试验证这些值是否存储到setter中时,它只告诉我setter是空的。我真的不明白为什么!有人知道为什么吗C++ c++;用另一个类的指针设置一个类的值,c++,pointers,setter,C++,Pointers,Setter,我试图实现游戏交易或不交易,我有两个类,方块和球员。盒子里储存的是矢量游戏盒,我想在玩家里面储存盒子和钱,玩家必须保留到游戏结束 我曾尝试以这种方式实施它。它运行正确,没有错误,但是当我尝试验证这些值是否存储到setter中时,它只告诉我setter是空的。我真的不明白为什么!有人知道为什么吗 class Box { vector<Box> game_box; float pound_contained; int box_number; public: Box(int box_
class Box
{
vector<Box> game_box;
float pound_contained;
int box_number;
public:
Box(int box_number, float pound_contained);
Box();
int getbox_number();
float getpound_contained();
};
class Player :public Box
{
int player_box;
float player_money;
public:
Player();
~Player();
float getplayer_money();
int getplayer_box();
void setplayer_money(float);
void setplayer_box(int);
};
void Player::setplayer_money(float)
{
player_money = player_money;
}
void Player::setplayer_box(int)
{
player_box = player_box;
}
float Player::getplayer_money()
{
return this->player_money;
}
int Player::getplayer_box()
{
return this->player_box;
}
int main()
{
vector<Box> game_box;
srand(time(0));
int n;
Player money;
Box oper;
float myArray [22][2] = { { 0.01, 0 }, { 0.10, 0 }, { 0.50, 0 },
{ 1, 0 }, { 5, 0 }, { 10, 0 },
{ 50, 0 }, { 100, 0 }, { 250, 0 },
{ 500, 0 }, { 750, 0 }, { 1000, 0 },
{ 3000, 0 }, { 5000, 0 }, { 10000, 0 },
{ 15000, 0 }, { 20000, 0 }, { 35000, 0 },
{ 50000, 0 }, { 75000, 0 }, { 100000, 0 },
{ 250000, 0 }
};
//random assignation of pound value to the 22 boxes
for (int e = 1; e <22; e++)
{
int pos;
bool op = true;
while (op)
{
pos = rand() % 22 + 1;
if (myArray[pos][1] == 0)
{
myArray[pos][1] = 1;
op = false;
}
}
Box b(e, myArray[pos][0]); //creating the class game
game_box.push_back(b); //function of the vector to insert a data in it
}
// random assignment of a box to the player
int i = rand() % 22 + 1;
Box* boxes = &game_box[i];
cout << "Your box is going to be the box number: "<< boxes->getbox_number()<<endl;
////////////////////////////////////////////////////////////////////setter not working
money.setplayer_money(boxes->getpound_contained());
money.setplayer_box(oper.getbox_number());
cout << money.getplayer_box() << " " << money.getplayer_money() << endl << endl;
game_box.erase(game_box.begin()+i);
return 0;
}
类框
{
向量博弈盒;
所含浮球;
输入框号;
公众:
方框(整数方框号,包含浮动磅);
Box();
int getbox_number();
float getpound_contained();
};
职业球员:公共包厢
{
int播放器盒;
浮动玩家的钱;
公众:
Player();
~Player();
float getplayer_money();
int getplayer_box();
无效设置玩家的金钱(浮动);
无效设置播放框(int);
};
无效玩家::设置玩家\货币(浮动)
{
玩家钱=玩家钱;
}
void Player::setplayer_框(int)
{
播放器盒=播放器盒;
}
float Player::getplayer_money()
{
归还此->玩家钱;
}
int Player::getplayer_box()
{
返回此->播放器\u框;
}
int main()
{
向量博弈盒;
srand(时间(0));
int n;
玩家金钱;
盒子操作器;
float myArray[22][2]={{0.01,0},{0.10,0},{0.50,0},
{ 1, 0 }, { 5, 0 }, { 10, 0 },
{ 50, 0 }, { 100, 0 }, { 250, 0 },
{ 500, 0 }, { 750, 0 }, { 1000, 0 },
{ 3000, 0 }, { 5000, 0 }, { 10000, 0 },
{ 15000, 0 }, { 20000, 0 }, { 35000, 0 },
{ 50000, 0 }, { 75000, 0 }, { 100000, 0 },
{ 250000, 0 }
};
//将磅值随机分配给22个箱子
对于(int e=1;e
void Player::setplayer_money(float player_money)
{
this->player_money = player_money;
}
?那么:
void Player::setplayer_money(float player_money)
{
this->player_money = player_money;
}
C++中没有“设置器”或“吸气剂”,它们不同于其他方法调用。因此,你可以像其他方法一样写它们。
为了详细说明@maniek指出的问题,您写道:
void Player::setplayer_money(float)
{
player_money = player_money;
}
C和C++有能力指定方法和函数参数,而不是名称——只是类型。这似乎有点奇怪,因为没有办法访问该参数(至少不是保证在所有编译器或优化级别中工作的方式,您可以通过它来完成)。。因此,您在这里所做的只是将成员
player\u money
设置为自身
(注意:如果您想知道为什么它允许您指定一个没有名称的方法参数,那么它有几个用途……一个是不命名它会抑制您没有使用该参数的警告消息。因此,这是一种将某些内容标记为当前未使用,但仍然是必需的方式——可能是出于遗留原因,也可能是因为您将来可能会使用它。)
您可以为参数指定一个与成员变量名称不重叠的新名称:
void Player::setplayer_money(float new_player_money)
{
player_money = new_player_money;
}
这是避免歧义的一种方法。因为就作用域中的值而言,参数将胜过成员变量。因此这将是另一个不做任何操作,将参数值分配给自身:
void Player::setplayer_money(float player_money)
{
player_money = player_money;
}
(注意:由于player_money是通过值传递的,而不是通过引用传递的,因此不会更改调用站点的参数值。特别是,如果传入一个常量,如10.20
,它如何更改值)
@maniek建议的是,在这种情况下消除歧义的一种方法是,当你指的是成员变量时,使用this->player\u money
,当你指的是方法的参数时,使用player\u money
。有些人做的另一件事是专门命名他们的成员变量——比如用m\u player\mo>开头ney
:
void Player::setplayer_money(float player_money)
{
m_player_money = player_money;
}
(注释:只要下一个字符是小写的,你也可以使用一个下划线前缀,没有<代码> M <代码>,但是有些人认为,下划线太危险了,后面的大写字母是留给编译器内部用法的。)
最后考虑一下——如果类名是
Player
,那么它已经隐含了你在设置谁的钱(玩家的钱),所以你可以直接称它为set\u money
。此外,我不喜欢在名称中加下划线(在C中比C++更常见),所以我可能会称它为setMoney
没有“setter”或“setter”C++中的“吸气剂”与其他方法调用不同。因此,你可以像其他方法一样写它们。
为了详细说明@maniek指出的问题,您写道:
void Player::setplayer_money(float)
{
player_money = player_money;
}
C和C++有能力指定方法和函数参数,而不是名称——只是类型。这似乎有点奇怪,因为没有办法访问该参数(至少不是保证在所有编译器或优化级别中工作的方式,您可以通过它来完成)。。因此,您在这里所做的只是将成员
player\u money
设置为自身
(注意:如果您想知道为什么它允许您指定一个没有名称的方法参数,那么它有几个用途……一个是不命名它会抑制您没有使用该参数的警告消息。因此,这是一种将某些内容标记为当前未使用,但仍然是必需的方式——可能是出于遗留原因,也可能是因为您将来可能会使用它。)
您可以为参数指定一个与成员变量名称不重叠的新名称:
void Player::setplayer_money(float new_player_money)
{
player_money = new_player_money;
}
这是避免歧义的一种方法。因为就作用域中的值而言,参数将胜过成员变量。因此这将是另一个不做任何操作,将参数值分配给自身:
void Player::setplayer_money(float player_money)
{
player_money = player_money;
}
(注意:因为player_money是通过值传递的,而不是通过引用传递的,所以这不会改变调用站点的参数值