vector of unique_pointers error?

Hi
I'm trying to declare this vector of unique pointers, in my class:
std::vector<std::unique_ptr<MyComponent>> m_pComponents;

but when i do, it gives me this super long error, that i cant seem to decipher.

heres of some of it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
M:\Include/MyEntity.hpp:13:7:   required from here
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/stl_algobase.h:340:18: error: use of delete
        *__result = *__first;
                  ^
In file included from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/memory:81:0,
                 from M:\Include/MyGame.hpp:3,
                 from M:\Src\mainer.cpp:1:
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/unique_ptr.h:357:19: note: declared here
       unique_ptr& operator=(const unique_ptr&) = delete;
                   ^
In file included from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/memory:62:0,
                 from M:\Include/MyGame.hpp:3,
                 from M:\Src\mainer.cpp:1:
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/stl_algobase.h: In instantiation of 'static
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/stl_algobase.h:400:44:   required from '_OI
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/stl_algobase.h:436:45:   required from '_OI
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/stl_algobase.h:469:8:   required from '_OI
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/vector.tcc:211:17:   required from 'std::ve
M:\Include/MyEntity.hpp:13:7:   required from here
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/stl_algobase.h:340:18: error: use of delete
        *__result = *__first;
                  ^ 


Heres my full code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma once

#include <vector>
#include <memory>
#include <bitset>
#include <array>
#include <iostream>

#include "MyComponent.hpp"

class MyScene;

class MyEntity
{
   private:
      std::vector<std::unique_ptr<MyComponent>> m_pComponents;

      std::bitset<MAX_COMPONENTS> m_componentBitset;
      std::array<MyComponent*, MAX_COMPONENTS> m_componentArray;




   public:
      MyScene* Scene;
      bool Enabled = true;

      virtual void Init()
      {
         for(auto& c : this->m_pComponents)
            c->Init();

      }

      virtual void Update()
      {
         for(auto& c : this->m_pComponents)
            if(c->Enabled)
               c->Update();

      }

      virtual void FixedUpdate()
      {
         for(auto& c : this->m_pComponents)
            if(c->Enabled)
               c->FixedUpdate();

      }

      template<typename T> 
      T& AddComponent()
      {
         static_assert(!Has<T>(), "TRYING TO ADD MULTIPLE OF SAME TYPE COMPONENTS");

         T* comp = new T();
         comp->Entity = this;

         m_componentBitset[GetTypeID<T>()] = true;
         m_componentArray[GetTypeID<T>()] = comp;

         std::unique_ptr<T> uPtr{comp};
         m_pComponents.push_back(std::move(uPtr));

         return *comp; 
      }


      template <typename T>
      bool Has()
      {
         return m_componentBitset[GetTypeID<T>()];
      }

      template <typename T>
      T& Get() 
      {
         MyComponent* comp(m_componentArray[GetTypeID<T>()]);

         return *((T*)comp);
      }


};



It used to be a simple vector of just raw pointers
but i would like to change it so that i doesn't leak memory.

Does anybody know what the error is about and how i can fix it?
Last edited on
As a guess it looks like you're trying to assign a unique_ptr<> somewhere in your code which is not allowed.


Last edited on
thanks for the reply
yeah, that is whats confusing me, because the only place i add something
to the vector is on line 63, and i made sure to use std::move there :\

do you think using shared_pointers would work too?
it doesn't seem to give me any errors when i define the vector as
 
std::vector<std::shared_ptr<MyComponent>> m_pComponents;
Last edited on
You aren't making any copies of MyEntity objects by any chance?
What is this?
1
2
3
4
5
6
7
8
      template<typename T> 
      T& AddComponent()
      {
         static_assert(!Has<T>(), "TRYING TO ADD MULTIPLE OF SAME TYPE COMPONENTS");

         T* comp = new T();
         comp->Entity = this;


You haven't provided enough code for us to do anything other than guess. If you want solid answers you're going to need to post more information. The code you provided is not the "whole code" it is only a snippet of the code.



The error says that your file mainer.cpp has #include "MyGame.hpp" at line 1.
And MyGame.hpp has #include <memory> at line 3
And memory has #include <stl_algobase.h> at line 81
And algobase.h has code that assigns one unique_ptr to another. at line 340.

I wonder if it's even possible to have a vector<unique_ptr<> > Vector<> has a copy constructor and assignment operator. How would those work when the template type can't be copied?

M:\Include/MyEntity.hpp:13:7: required from here
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/stl_algobase.h:340:18: error: use of delete
*__result = *__first;
^
In file included from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/memory:81:0,
from M:\Include/MyGame.hpp:3,
from M:\Src\mainer.cpp:1:
I wonder if it's even possible to have a vector<unique_ptr<> > Vector<> has a copy constructor and assignment operator. How would those work when the template type can't be copied?

It is possible. std::vector only requires the elements to be movable. If the elements are not copyable it means you will not be able to copy the vector but you can still move it.
Last edited on
Registered users can post here. Sign in or register to post.