I have an entity class which makes use of a vector of unique pointers. For some reason it is giving the following error:
Error log:
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&& ...) [with _T1 = std::unique_ptr<IComponent>; _Args = {const std::unique_ptr<IComponent, std::default_delete<IComponent> >&}]’:
/usr/include/c++/8/bits/stl_uninitialized.h:83:18: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<IComponent>*, std::vector<std::unique_ptr<IComponent> > >; _ForwardIterator = std::unique_ptr<IComponent>*; 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<const std::unique_ptr<IComponent>*, std::vector<std::unique_ptr<IComponent> > >; _ForwardIterator = std::unique_ptr<IComponent>*]’
/usr/include/c++/8/bits/stl_uninitialized.h:289:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<IComponent>*, std::vector<std::unique_ptr<IComponent> > >; _ForwardIterator = std::unique_ptr<IComponent>*; _Tp = std::unique_ptr<IComponent>]’
/usr/include/c++/8/bits/stl_vector.h:463:31: required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<IComponent>; _Alloc = std::allocator<std::unique_ptr<IComponent> >]’
./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<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = IComponent; _Dp = std::default_delete<IComponent>]’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__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&) = delete;
^~~~~~~~~~
Entity class
class IEntity{
private:
bool active = true;
vector<unique_ptr<IComponent>> components;
ComponentArray componentArray;
ComponentBitset componentBitset;
public:
const char* name;
template<typename T> bool hasComponent(){
return componentBitset[getComponentTypeID<T>()];
}
template<typename T> T& addComponent(){
T c;
unique_ptr<T> uPtr(&c);
components.emplace_back(move(uPtr));
componentArray[getComponentTypeID<T>()] = &c;
componentBitset[getComponentTypeID<T>()] = true;
return *move(uPtr);
}
template<typename T> T& getComponent() const{
auto ptr(componentArray[getComponentTypeID<T>()]);
return *static_cast<T*>(ptr);
}
};
I know that unique pointers can't be copied but I can't figure out where I am copying it.
Edit: I actually got where it was getting copied by adding the following code to IEntity:
explicit IEntity();
IEntity(const IEntity&) = delete;
IEntity& operator= (const IEntity&) = delete;
~IEntity() = default;
But now it is showing error at a function given below:
//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<MeshRenderer>().shader;
UniformCaller(shader->uniform_callback, object, *shader);
}
//Definition
void UniformCaller(void (*ptr)(IEntity object, Shader shader), IEntity object, Shader shader){
(*ptr) (object, shader);
}
Edit2: As pointed in comments I changed all of them to references and now I get a wierd error as follows:
/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
addEntity():
static IEntity& addEntity(){
IEntity* e = new IEntity();
unique_ptr<IEntity> uPtr(e);
entities.emplace_back(move(uPtr));
return *move(uPtr);
}
createComponent():
static IComponent& createComponent(IComponent* component){
unique_ptr<IComponent> ptr(component);
components.emplace_back(move(ptr));
return *move(ptr);
}