从 vector 访问时,我的可变类的属性不会更改。(调试中)
The properties of my mutable class aren't being changed when accessed from vector. (Debugging)
我真的在调试这段代码时度过了一段地狱般的时光。
编写此代码是为了将棋盘上的每个玩家向前移动,直到其中一个玩家到达终点,但看起来 Player::p osition 实际上从未更改过。我已经尝试过使用MinGW C++11和安装在带有标志-std=c++11
的ubuntu遥控器上的g ++。
请提出问题或提出建议,我只是不明白。
#include <iostream>
#include <cstdlib>
#include <vector>
#include <time.h>
using namespace std;
class Player {
string name;
int position;
public:
Player (string p_name):
name(p_name),
position(1)
{
};
int move (int diff) {
position = diff + position;
return position;
};
int get_position () { return position; };
string get_name () { return name; }
};
class GameBoard {
private:
vector<Player> players;
int total_players;
int turn_index;
bool has_winner;
int winning_space;
int dice_sides;
int max_penalty_spaces;
int min_penalty_spaces;
int roll_dice () { return rand() % dice_sides + 1; };
public:
GameBoard (vector<Player> & p_players):
players(p_players),
total_players(p_players.size()),
turn_index(-1),
has_winner(false),
winning_space(20),
dice_sides(6),
max_penalty_spaces(3),
min_penalty_spaces(1)
{
};
bool get_has_winner () { return has_winner; };
Player get_last_player () { return players[turn_index]; };
void move_next_player () {
turn_index = (turn_index + 1) % total_players;
Player current_player = players[turn_index];
cout << ¤t_player << " - testn";
int spaces_to_move = roll_dice();
int player_space = current_player.move(spaces_to_move);
if (player_space >= winning_space) {
has_winner = true;
} else {
cout << current_player.get_name() << " moved forward " << spaces_to_move << " spaces to space: " << player_space << ".n";
// Iterate through other players
for (int i = 1; i < total_players - 1; ++i) {
Player other_player = players[(turn_index + i) % total_players];
if (player_space == other_player.get_position()) {
// Player lands on other player's position
int spaces_back = rand() % (max_penalty_spaces - min_penalty_spaces + 1) + min_penalty_spaces;
other_player.move(-1 * spaces_back);
cout << current_player.get_name() << " landed on " << other_player.get_name() << "'s space, causing ";
cout << other_player.get_name() << " to take a tumble and move backwards " << spaces_back << " spaces.n";
}
}
cout << current_player.get_name() << current_player.get_position() << " " << ¤t_player << " - testn";
}
}
};
int main () {
srand(time(NULL));
vector<Player> players {
Player("Jack"),
Player("Jill")
};
GameBoard game{players};
while (game.get_has_winner() != true) {
game.move_next_player();
}
Player winner = game.get_last_player();
cout << winner.get_name() << " has won!n";
//delete[] &players;
//delete &game;
return 0;
}
输出也很有趣:
Jack moved forward 6 spaces to space: 7.
Jack7 0x7fffd1d9a6b0 - test
0x7fffd1d9a6b0 - test
Jill moved forward 4 spaces to space: 5.
Jill5 0x7fffd1d9a6b0 - test
0x7fffd1d9a6b0 - test
Jack moved forward 6 spaces to space: 7.
Jack7 0x7fffd1d9a6b0 - test
0x7fffd1d9a6b0 - test
显示杰克和吉尔共享同一个地址。
Player current_player = players[turn_index];
创建一个副本。然后你继续打电话给Player::move
current_player
.这将对对象players[turn_index]
没有影响。请改用引用,如下所示:
Player& current_player = players[turn_index];
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 在linux上调试巨大的C++项目
- 通过指向指针数组的指针访问子类的属性
- 为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
- MSVC是否支持C++11样式的属性而不是__declspec
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 正在VS调试器中监视映射条目
- 使用调试/崩溃报告将应用程序部署到客户端
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- VC++本机单元测试,找不到调试符号
- Qt Quick-如何仅从c++代码与qml属性交互
- 如何在C++生成器中禁用"使用调试.dcus"
- C++ Visual Studio 解决方案的共享属性表(调试和发布)
- Visual Studio 2010 默认属性表似乎在调试和发布配置下都链接在一起,这是怎么回事?
- 从 vector 访问时,我的可变类的属性不会更改。(调试中)
- 为属性表创建调试友好的枚举