首页 > 解决方案 > 根据用户输入的玩家数量创建玩家对象?

问题描述

背景:
我创建了一个玩家类,我想问用户有多少玩家要玩这个游戏?根据用户输入,我正在尝试创建播放器类的许多实例。但是,我使用以下链接来帮助我:
使用用户输入创建对象
http://www.cplusplus.com/forum/beginner/197342/
所以,我尝试了他们的解决方案:

#include "Player.h"

int main() {

    int totalPlayers = -1;
    cout << "Enter total number of players: ";
    while ((totalPlayers < 1) && (totalPlayers > 5)) {
        cout << "How many players will be playing? (1-5): ";
        cin >> totalPlayers;
    }
    vector<Player> players(totalPlayers);

    system("pause");
}    

我收到错误:16LinearChess.exe 中 0x763F40B2 处的未处理异常:Microsoft C++ 异常:内存位置 0x003BF690 处的 std::length_error。
所以,我用谷歌搜索了这个确切的错误并找到了这个链接:错误:内存位置的 std::length_error
所以,首先他的代码与我的代码无关,但错误是一样的。我不明白答案,但我认为我必须使用堆内存创建实例。所以我尝试了:

#include "Player.h"

int main() {

    int totalPlayers = -1;
    cout << "Enter total number of players: ";
    while ((totalPlayers < 1) && (totalPlayers > 5)) {
        cout << "How many players will be playing? (1-5): ";
        cin >> totalPlayers;
    }
    vector<Player> *players = new Player(totalPlayers);
    delete[] players;

    system("pause");
}    

我有两个错误:
严重性代码描述项目文件行抑制状态错误(活动)E0144 类型“播放器 *”的值不能用于初始化类型为“std::vector<Player, std::allocator> *”的实体16LinearChess D:\Keshav\Programming Languages\C++\Beginner\01 Michael Dawson\16LinearChess\LinearChess.cpp 64


严重性代码描述项目文件行抑制状态错误(活动) E0289 构造函数“Player::Player”的实例不匹配参数列表16LinearChess D:\Keshav\Programming Languages\C++\Beginner\01 Michael Dawson\16LinearChess\LinearChess.cpp

这是我的播放器类:

#include <iostream>

class Player : public Board {
protected:
    int m_position;
    Log logger;
    int m_playerNumber;
public: 
    static int m_numberOfPlayers;
    Player() :m_position(0) {
        ++m_numberOfPlayers; 
        m_playerNumber = m_numberOfPlayers;
    }

    void m_SetPlayerPosition(int &position) {
        if ((position < 0) || (position > 100)) {
            m_position = 0;
            logger.Error("Position cannot be less than or greater than 100. Your position has been reset to 0 because you fell out of the map.");
        }
        else {
            m_position = position;
        }
        m_SetBoardPosition(m_position, m_numberOfPlayers); // update the position on the board.
    }

    friend ostream &operator << (ostream &os, Player &player) {
        os << "Player position on board: " << player.m_position << "\nPlayer Number: " << player.m_playerNumber << '\n';
        return os;
    }
};

int Player::m_numberOfPlayers = 0; // initializing total number of players.

谢谢你!

标签: c++oopvector

解决方案


问题在于您的 while 循环:

    int totalPlayers = -1;
    cout << "Enter total number of players: ";
    while ((totalPlayers < 1) && (totalPlayers > 5)) {
        cout << "How many players will be playing? (1-5): ";
        cin >> totalPlayers;
    }

while 循环只会在条件为真时运行,但条件永远不会为真,因为任何数字都不能小于 1,也不能大于 5。由于条件不为真,while 循环将永远不会运行,并且totalPlayers将等于-1,如果您尝试访问数组索引,这是您永远不会想要的。

把它改成这样:totalPlayers < 1 || totalPlayers > 5用一个||代替,你应该没问题。

对于错误:

Unhandled exception at 0x763F40B2 in 16LinearChess.exe: Microsoft C++ exception: std::length_error at memory location 0x003BF690.

您的代码抛出异常,因为totalPlayers等于 -1。所以你基本上是这样做的:

vector<Player> players(-1);

这没有任何意义,因为您正在创建一个包含 -1 元素的数组?所以代码抛出了一个异常,告诉你出了点问题。std::length_error应该提示什么是错的。

也像许多评论所说的那样,不要这样做:

vector<Player> *players = new Player(totalPlayers);

a 的全部目的vector是让你不要那样做。你的第一个例子工作正常:

vector<Player> players(totalPlayers);

推荐阅读