title: Instantiating object as a class attribute with custom constructor (C++)
body: <p>I'm writing a standard game of Battleship using C++, with a Game object that contains two Player objects within it. When I try to instantiate the Player objects in the Game constructor, IntelliSense gives me two errors:</p>
  <blockquote>
  <p>IntelliSense: expression must be a modifiable lvalue</p>
  <p>IntelliSense: no suitable constructor exists to convert from &quot;Player ()&quot; to &quot;Player&quot;</p>
  </blockquote>
  <p>Here's my header file:</p>
  <pre><code>class Player {
  public:
      Player(string name);
      //More unrelated stuff (Get/Set methods and Attributes)
  };
  
  
  class Game {
  public:
      Game(bool twoPlayer, string Player1Name, string Player2Name);
      //Get and Set methods (not included)
      //Attributes:
      Player Player1();
      Player Player2();
      int turn;
  };
  </code></pre>
  <p>My definition of the Player constructor:</p>
  <pre><code>Player::Player(string name)
  {
      SetName(name);
      //Initialize other variables that don't take input
  {
  </code></pre>
  <p>And the code that's giving the errors:</p>
  <pre><code>//Game constructor
  Game::Game(bool twoPlayer, string Player1Name, string Player2Name)
  {
      Player1 = Player(Player1Name);  //These two lines give the first error
      Player2 = Player(Player2Name);
      turn = 1;
  }
  
  //Game class Gets
  int Game::GetTurn() { return turn; }
  Player Game::GetPlayer1() { return Player1; }  //These two lines give the second error
  Player Game::GetPlayer2() { return Player2; }
  </code></pre>
  <p>What am I doing wrong? I've tried changing</p>
  <pre><code>Player1 = Player(Player1Name);
  Player2 = Player(Player2Name);
  </code></pre>
  <p>to</p>
  <pre><code>Player1 Player(Player1Name);
  Player2 Player(Player2Name);
  </code></pre>
  <p>and a number of other things, but nothing works. Thanks a lot in advance!</p>