title: Have const variables in an object which is used in a vector container
body: <p>So I am trying to make snake clone. </p>
  
  <p>I have made a BodyPiece class which has certain variables I would like to be const, as they should not change. This was not a problem when I simply created an instance of Bodypiece to test out. However I want to be able to add pieces as the snake grows so I made a vector body variable. As you can see from the code below:</p>
  
  <p>In BodyPiece.h:</p>
  
  <pre><code>#pragma once
  #include "D3DGraphics.h"
  #include "Keyboard.h"
  #include "Direction.h"
  
  class BodyPiece
  {
  public:
      BodyPiece(int xPos, int yPos, Direction* dir);
      ~BodyPiece();
      void Draw(D3DGraphics&amp; gfx);
      void UpdateDirection(KeyboardClient&amp; kbd);
      void Move();
  private:
      Direction* dir;
      int xPos;
      int yPos;
      int width = 5;
      int height = 5;
      int vx = 5;
      int vy = 5;
  };
  </code></pre>
  
  <p>In Game.h file</p>
  
  <p>BodyPiece vector body declared:</p>
  
  <pre><code>std::vector&lt;BodyPiece&gt; body;
  </code></pre>
  
  <p>In Game.cpp file</p>
  
  <p>vector initialised in initialiser list.</p>
  
  <pre><code>body({ {400, 300, &amp;d } })
  </code></pre>
  
  <p>If I make any variables in BodyPiece const, it will generate an error: </p>
  
  <p>error C2582 'operator=' function is unavailable in class BodyPiece.</p>
  
  <p>My questions here are: </p>
  
  <p>What causes that? I have looked online and have a little bit of an idea but it really is only a little bit. </p>
  
  <p>How can I have const variables, use a vector container(or another type of container if that will help, does not have to be vector) and keep the compiler happy? Or should I just completely change my approach?</p>
  
  <p>Thank you in advance.</p>