Assignment operator implicitly deleted

Hi

I just received the following error message which I'm struggling to fix:

1
2
3
4
5
6
7
8
*__result = std::move(*__first);
        ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
In file included from Container.h:6:0,
                 from Container.cpp:5:
Element.h:42:7: note: 'Element& Element::operator=(Element&&)' is implicitly deleted because the default definition would be ill-formed:
 class Element{
       ^~~~~~
Element.h:42:7: error: non-static reference member 'Container& Element::r_ent_register', can't use default assignment operator 


I think the issue is something to do with the constness of Element and this causing problems with push_back (which uses std::move).

For reference the Container object has a vector of Elements. These elements can be accessed with the following methods (const and non-const):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const Element& Container::operator[](ElementID input_ent_id) const {
  for (const Element& ent : ent_register) {
    if (ent.getId() == input_ent_id) {
      return ent;
    }
  }
}

Element& Container::operator[](ElementID input_ent_id) {
  for (Element& ent : ent_register) {
    if (ent.getId() == input_ent_id) {
      return ent;
    }
  }
}


Elements are added to Container via the following Container method:
1
2
3
4
5
ElementID Container::addElement(const std::string& nm, EType ent_type) {
  Element ent(nm, ent_type, *this, generateUniqueID());
  ent_register.push_back(ent);
  return ent.getId();
}


Does anyone have any ideas? Let me know if you need further extracts of code.


Thanks
I think the issue is something to do with the constness of Element
Element.h:42:7: error: non-static reference member 'Container& Element::r_ent_register', can't use default assignment operator

https://en.cppreference.com/w/cpp/language/copy_assignment
Deleted implicitly-declared copy assignment operator

A implicitly-declared copy assignment operator for class T is defined as deleted if any of the following is true:
• T has a user-declared move constructor;
• T has a user-declared move assignment operator.

Otherwise, it is defined as defaulted.

A defaulted copy assignment operator for class T is defined as deleted if any of the following is true:

• T has a non-static data member of non-class type (or array thereof) that is const;
• T has a non-static data member of a reference type;
• T has a non-static data member or a direct or virtual base class that cannot be copy-assigned (overload resolution for the copy assignment fails, or selects a deleted or inaccessible function);
• T is a union-like class, and has a variant member whose corresponding assignment operator is non-trivial.
(bold italic is mine)

Perhaps is because there is a reference property (see sentence in bold italic).
Topic archived. No new replies allowed.