从 vector 访问时,我的可变类的属性不会更改。(调试中)

The properties of my mutable class aren't being changed when accessed from vector. (Debugging)

本文关键字:调试 属性 访问 vector 我的      更新时间:2023-10-16

我真的在调试这段代码时度过了一段地狱般的时光。

编写此代码是为了将棋盘上的每个玩家向前移动,直到其中一个玩家到达终点,但看起来 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 << &current_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() << "  " << &current_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];