lundi 3 octobre 2016

Object of type cannot be assigned because its copy assignment operator is implicitly deleted

I have written a simple vector implementation and in it I have the following code:

void push_back(const T& elem)
    {
        resize(cont_size + 1);
        container[cont_size - 1] = elem;
    }

and

MyCustomVector &operator=(const MyCustomVector &src)
    {
        resize(src.cont_size);
        memcpy(container, src.container, cont_size * sizeof(T));
        return *this;
    }

in my main.cpp, I have:

MyCustomVector<std::unique_ptr<MyObject>> container;
container.push_back(std::make_unique<MyObject>(arg1, arg2, arg3, arg4));

When I compile my program, I get the following error:

error: object of type 'std::__1::unique_ptr<MyObject,
  std::__1::default_delete<MyObject> >' cannot be assigned because its copy
  assignment operator is implicitly deleted

copy assignment operator is implicitly deleted because
'unique_ptr<MyObject,
  std::__1::default_delete<MyObject> >' has a user-declared move constructor
_LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT

I have tried replacing container[cont_size - 1] = elem with container[cont_size - 1] = std::move(elem) but it doesn't help.

Can someone help me out with this issue? I don't fully seem to understand what I am doing wrong.

Aucun commentaire:

Enregistrer un commentaire