title: Use of deleted function unique_ptr
body: <p>I have an entity class which makes use of a vector of unique pointers. For some reason it is giving the following error:</p>
  
  <p>Error log:</p>
  
  <pre><code>In file included from /usr/include/c++/8/vector:62,
                   from ./RactorUtils.h:8,
                   from ./OpenGLRenderer.h:4,
                   from ./TestGame.cpp:2:
  /usr/include/c++/8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&amp;&amp; ...) [with _T1 = std::unique_ptr&lt;IComponent&gt;; _Args = {const std::unique_ptr&lt;IComponent, std::default_delete&lt;IComponent&gt; &gt;&amp;}]’:
  /usr/include/c++/8/bits/stl_uninitialized.h:83:18:   required from ‘static _ForwardIterator std::__uninitialized_copy&lt;_TrivialValueTypes&gt;::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator&lt;const std::unique_ptr&lt;IComponent&gt;*, std::vector&lt;std::unique_ptr&lt;IComponent&gt; &gt; &gt;; _ForwardIterator = std::unique_ptr&lt;IComponent&gt;*; bool _TrivialValueTypes = false]’
  /usr/include/c++/8/bits/stl_uninitialized.h:134:15:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator&lt;const std::unique_ptr&lt;IComponent&gt;*, std::vector&lt;std::unique_ptr&lt;IComponent&gt; &gt; &gt;; _ForwardIterator = std::unique_ptr&lt;IComponent&gt;*]’
  /usr/include/c++/8/bits/stl_uninitialized.h:289:37:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator&lt;_Tp&gt;&amp;) [with _InputIterator = __gnu_cxx::__normal_iterator&lt;const std::unique_ptr&lt;IComponent&gt;*, std::vector&lt;std::unique_ptr&lt;IComponent&gt; &gt; &gt;; _ForwardIterator = std::unique_ptr&lt;IComponent&gt;*; _Tp = std::unique_ptr&lt;IComponent&gt;]’
  /usr/include/c++/8/bits/stl_vector.h:463:31:   required from ‘std::vector&lt;_Tp, _Alloc&gt;::vector(const std::vector&lt;_Tp, _Alloc&gt;&amp;) [with _Tp = std::unique_ptr&lt;IComponent&gt;; _Alloc = std::allocator&lt;std::unique_ptr&lt;IComponent&gt; &gt;]’
  ./ECS/ECS.h:42:7:   required from here
  /usr/include/c++/8/bits/stl_construct.h:75:7: error: use of deleted function ‘std::unique_ptr&lt;_Tp, _Dp&gt;::unique_ptr(const std::unique_ptr&lt;_Tp, _Dp&gt;&amp;) [with _Tp = IComponent; _Dp = std::default_delete&lt;IComponent&gt;]’
       { ::new(static_cast&lt;void*&gt;(__p)) _T1(std::forward&lt;_Args&gt;(__args)...); }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  In file included from /usr/include/c++/8/memory:80,
                   from ./ECS/ECS.h:6,
                   from ./RactorUtils.h:23,
                   from ./OpenGLRenderer.h:4,
                   from ./TestGame.cpp:2:
  /usr/include/c++/8/bits/unique_ptr.h:394:7: note: declared here
         unique_ptr(const unique_ptr&amp;) = delete;
         ^~~~~~~~~~
  </code></pre>
  
  <p>Entity class</p>
  
  <pre><code>class IEntity{
  private:
      bool active = true;
      vector&lt;unique_ptr&lt;IComponent&gt;&gt; components;
      ComponentArray componentArray;
      ComponentBitset componentBitset;
  
  public:
      const char* name;
  
      template&lt;typename T&gt; bool hasComponent(){
          return componentBitset[getComponentTypeID&lt;T&gt;()];
      }
  
      template&lt;typename T&gt; T&amp; addComponent(){
          T c;
          unique_ptr&lt;T&gt; uPtr(&amp;c);
          components.emplace_back(move(uPtr));
  
          componentArray[getComponentTypeID&lt;T&gt;()] = &amp;c;
          componentBitset[getComponentTypeID&lt;T&gt;()] = true;
  
          return *move(uPtr);
      }
  
      template&lt;typename T&gt; T&amp; getComponent() const{
          auto ptr(componentArray[getComponentTypeID&lt;T&gt;()]);
          return *static_cast&lt;T*&gt;(ptr);
      }
  };
  </code></pre>
  
  <p>I know that unique pointers can't be copied but I can't figure out where I am copying it.</p>
  
  <p>Edit: I actually got where it was getting copied by adding the following code to IEntity:</p>
  
  <pre><code>explicit IEntity();
  IEntity(const IEntity&amp;) = delete;
  IEntity&amp; operator= (const IEntity&amp;) = delete;
  ~IEntity() = default;
  </code></pre>
  
  <p>But now it is showing error at a function given below:</p>
  
  <pre><code>//Prototype
  void UniformCaller(void (*ptr)(IEntity object, Shader shader), IEntity object, Shader shader);
  
  //Call
  void Render(IEntity object, glm::mat4x4 projectionMatrix, glm::mat4x4 viewMatrix){
  Shader* shader = object.getComponent&lt;MeshRenderer&gt;().shader;
  UniformCaller(shader-&gt;uniform_callback, object, *shader);
  }
  
  //Definition
  void UniformCaller(void (*ptr)(IEntity object, Shader shader), IEntity object, Shader shader){
      (*ptr) (object, shader);
  }
  </code></pre>
  
  <p>Edit2: As pointed in comments I changed all of them to references and now I get a wierd error as follows:</p>
  
  <pre><code>/usr/bin/ld: /tmp/ccPzMwMa.o: in function `ComponentManager::createComponent(IComponent*)':
  TestGame.cpp:(.text._ZN16ComponentManager15createComponentEP10IComponent[_ZN16ComponentManager15createComponentEP10IComponent]+0x41): undefined reference to `ComponentManager::components'
  /usr/bin/ld: /tmp/ccPzMwMa.o: in function `EntityManager::addEntity()':
  TestGame.cpp:(.text._ZN13EntityManager9addEntityEv[_ZN13EntityManager9addEntityEv]+0x6c): undefined reference to `EntityManager::entities'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>addEntity():</p>
  
  <pre><code>static IEntity&amp; addEntity(){
          IEntity* e = new IEntity();
          unique_ptr&lt;IEntity&gt; uPtr(e);
          entities.emplace_back(move(uPtr));
          return *move(uPtr);
  }
  </code></pre>
  
  <p>createComponent():</p>
  
  <pre><code>static IComponent&amp; createComponent(IComponent* component){
          unique_ptr&lt;IComponent&gt; ptr(component);
          components.emplace_back(move(ptr));
          return *move(ptr);
  }
  </code></pre>