This answer is based on the correct suggestion by @Praetorian.
Short answer with a fix:
The issue is in the std::vector<Pickup>& copy;
member of the Enemy
class. Change that to std::vector<Pickup> copy;
, removing the reference, to fix the error. Indeed, there is absolutely no need to store the vector
as a reference, on the contrary, as you can see, having reference members in a class cause errors like the one you've faced. You may have been confused by the fact that the vector
is passed by reference to the initialization c'tor, but it in no way means that you should also make the class member a reference.
If you still want to store the Enemy::copy
data by reference, for e.g. performance issues, consider making it a pointer, like so:
class Enemy : public Entity
{
private:
std::vector<Pickup>* copy;
// rest of the class...
};
Long answer:
Code to reproduce the issue:
class Foo
{
int& ri;
public:
// default c'tor can't be defined in a sane fashion because of the reference member ri
// Foo() : ri(0) {}
Foo(int& _ri) : ri(_ri) {}
};
int main(int argc, char **argv)
{
int i{42};
Foo f1{i};
Foo f2{f1}; // we don't have a default constructor so using auto-generated copy c'tor
f2 = f1; // <--- error
return 0;
}
Results in the error:
main.cpp: In function 'int main(int, char**)':
main.cpp:14:12: error: use of deleted function 'Foo& Foo::operator=(const Foo&)'
f2 = f1; // <--- error
^
main.cpp:1:11: note: 'Foo& Foo::operator=(const Foo&)' is implicitly deleted
because the default definition would be ill-formed:
class Foo
^
main.cpp:1:11: error: non-static reference member 'int& Foo::ri',
can't use default assignment operator
Explanation:
An auto-generated operator=()
, which would otherwise be generated in this case [1], is considered to be ill-formed
due to the fact the you can't reassign references in C++ [2]. The auto-generated operator=(Foo& other)
, should it exist, would attempt to perform this->ri = other.ri
, which is considered to be incorrect ("ill-formed", as reported by the compiler), since it's a re-assignment of the reference ri
.
Finally, I suggest to see [3], with an in-depth answer on the issue.
about auto-generated copy assignment operator
about re-assigning references
Assignment operator and copy constructor in the presence of references