c++ - 从 vector 访问时,我的可变类的属性没有改变。 (调试)

标签 c++ c++11

我在调试这段代码时简直是 hell 般的时光。

编写这段代码是为了让每个玩家都在棋盘上,并让他们向前移动,直到其中一个玩家到达终点,但实际上 Player::position 看起来从未真正改变过。我已经用标志 -std=c++11 安装在我的 ubuntu Remote 上的 MinGW C++11 和 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 << " - test\n";
            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 << " - test\n";
            }
        }
};

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

显示 Jack 和 Jill 共享同一个地址。

最佳答案

Player current_player = players[turn_index]; 创建一个拷贝。然后继续调用 current_player 上的 Player::move。这对对象 players[turn_index] 没有影响。使用这样的引用:

Player¤t_player = players[turn_index];

关于c++ - 从 vector 访问时,我的可变类的属性没有改变。 (调试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28185127/

相关文章:

c++ - 调试共享内存的内容

c# - PInvoke:返回的 double 组有问题吗?

c++ - 返回不同的数据类型而不明确指定数据类型

C++模板非类型参数lambda函数

c++ - 继承默认构造函数在 gcc 中失败,但在 clang 中有效,哪个有 bug?

c++ - 在 C++11 中查找和访问优先级队列中的元素

c++ - operator+ 序列中临时对象的生命周期

c++ - 如何检查宏参数中是否有赋值?

c++ - 什么时候将数组转换为指针?

c++ - 如何将 shared_ptr 传递给生命周期较短的类?