I am trying to create an Entity which can hold a pointer to its parent and a vector to its children.
The problem is when I try to emplace_back or push_back to the vector of children, I get
Error C2280 'Entity::Entity(const Entity &)': attempting to reference a deleted function
Due to the fact I have a unique_ptr in the Entity.
I thought that adding a move-constructor would solve this problem but it has not.
I have included a minimal verifiable runnable example here below.
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
struct Entity
{
//////// data
unique_ptr<Entity> mParent;
std::vector<Entity> mChildren;
//////// ctors
// default
Entity() = default;
// move
Entity(Entity && entity): mParent{std::move(entity.mParent)}{}
//////// functions
void add_child(Entity const && entity)
{
mChildren.emplace_back(entity); // COMMENT OUT THIS LINE FOR FUNCTIONAL CODE
//Error C2280 'Entity::Entity(const Entity &)': attempting to reference a deleted function in... include\xmemory0 881
};
};
int main()
{
Entity entity;
entity.add_child(Entity());
return 0;
}