dimanche 17 décembre 2017

Vector how to erase object that is not move-assignable

class A {
public:
  A(const A&) = delete;
  A& operator=(const A&) = delete;
}

class B : public A{
public:
  B(C &ref):c(ref){}
private:
  C &c;
}

std::vector<B> vec;
....  // add elements
vec.erase(vec.begin());

When I use vec.erase , it fails when i try to compile it. and the error is:

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

I check the erase requirement and find move assignment could solve it. So I offer move assignment, but i couldn't add a default constructor. It still doesn't work.

Is there any solution to make it erasable?

Aucun commentaire:

Enregistrer un commentaire