title: Create player objects based on the number of players from user input?
body: <p>Background:<br />
  I have created a player class and I want to ask the user how many players are going to play the game? Based on the user input I am trying to create that many instances of the player class. However, I used the following links to help me: <br />
  <a href="https://stackoverflow.com/questions/42092911/create-object-using-user-input">Create object using user input</a> <br />
  <a href="http://www.cplusplus.com/forum/beginner/197342/" rel="nofollow noreferrer">http://www.cplusplus.com/forum/beginner/197342/</a> <br />
  So, I tried their solution:</p>
  <pre><code>#include &quot;Player.h&quot;
  
  int main() {
  
      int totalPlayers = -1;
      cout &lt;&lt; &quot;Enter total number of players: &quot;;
      while ((totalPlayers &lt; 1) &amp;&amp; (totalPlayers &gt; 5)) {
          cout &lt;&lt; &quot;How many players will be playing? (1-5): &quot;;
          cin &gt;&gt; totalPlayers;
      }
      vector&lt;Player&gt; players(totalPlayers);
  
      system(&quot;pause&quot;);
  }    
  </code></pre>
  <p>I get the error: <b>Unhandled exception at 0x763F40B2 in 16LinearChess.exe: Microsoft C++ exception: std::length_error at memory location 0x003BF690.</b> <br />
  So, I googled this exact error and found this link: <a href="https://stackoverflow.com/questions/50964441/error-stdlength-error-at-memory-location">Error : std::length_error at memory location</a> <br />
  So, firstly his code was no-where related to mine, but the error was same. I did not understand the answer, but I thought that I had to create the instances using heap memory. So I tried that: <br /></p>
  <pre><code>#include &quot;Player.h&quot;
  
  int main() {
  
      int totalPlayers = -1;
      cout &lt;&lt; &quot;Enter total number of players: &quot;;
      while ((totalPlayers &lt; 1) &amp;&amp; (totalPlayers &gt; 5)) {
          cout &lt;&lt; &quot;How many players will be playing? (1-5): &quot;;
          cin &gt;&gt; totalPlayers;
      }
      vector&lt;Player&gt; *players = new Player(totalPlayers);
      delete[] players;
  
      system(&quot;pause&quot;);
  }    
  </code></pre>
  <p>I got two errors:<br /> <b>Severity Code    Description Project File    Line    Suppression State
  Error (active)  E0144   a value of type &quot;Player *&quot; cannot be used to initialize an entity of type &quot;std::vector&lt;Player, std::allocator&gt; *&quot;   16LinearChess   D:\Keshav\Programming Languages\C++\Beginner\01 Michael Dawson\16LinearChess\LinearChess.cpp    64<br />
  </b><br /><br />
  <b>Severity Code    Description Project File    Line    Suppression State
  Error (active)  E0289   no instance of constructor &quot;Player::Player&quot; matches the argument list   16LinearChess   D:\Keshav\Programming Languages\C++\Beginner\01 Michael Dawson\16LinearChess\LinearChess.cpp</b></p>
  <p>This is my Player class:</p>
  <pre><code>#include &lt;iostream&gt;
  
  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 &amp;position) {
          if ((position &lt; 0) || (position &gt; 100)) {
              m_position = 0;
              logger.Error(&quot;Position cannot be less than or greater than 100. Your position has been reset to 0 because you fell out of the map.&quot;);
          }
          else {
              m_position = position;
          }
          m_SetBoardPosition(m_position, m_numberOfPlayers); // update the position on the board.
      }
  
      friend ostream &amp;operator &lt;&lt; (ostream &amp;os, Player &amp;player) {
          os &lt;&lt; &quot;Player position on board: &quot; &lt;&lt; player.m_position &lt;&lt; &quot;\nPlayer Number: &quot; &lt;&lt; player.m_playerNumber &lt;&lt; '\n';
          return os;
      }
  };
  
  int Player::m_numberOfPlayers = 0; // initializing total number of players.
  </code></pre>
  <p>Thank You!</p>