c++ - Error attempting to reference a deleted function -
i want initialize enemy::copy using parameter passed initialization constructor, following error:
c2280: 'enemy &enemy::operator =(const enemy &)': attempting reference deleted function here declaration of class enemy:
#include "pickup.h" class enemy : public entity { private: std::vector<pickup>& copy; public: enemy::enemy(std::vector<pickup>& arraypickup) : copy(arraypickup) {} }; and code main():
std::vector<pickup> arraypickup; enemy enemy(arraypickup); how solve it?
this answer based on correct suggestion @praetorian.
short answer fix:
the issue in std::vector<pickup>& copy; member of enemy class. change std::vector<pickup> copy;, removing reference, fix error. indeed, there absolutely no need store vector reference, on contrary, can see, having reference members in class cause errors 1 you've faced. may have been confused fact vector passed reference initialization c'tor, in no way means should make class member reference.
if still want store enemy::copy data reference, e.g. performance issues, consider making pointer, so:
class enemy : public entity { private: std::vector<pickup>* copy; // rest of class... }; long answer:
code reproduce issue:
class foo { int& ri; public: // default c'tor can't defined in sane fashion because of 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}; // don't have default constructor using auto-generated copy c'tor f2 = f1; // <--- error return 0; } results in 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&)' implicitly deleted because default definition 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=(), otherwise generated in case [1], considered ill-formed due fact can't reassign references in c++ [2]. auto-generated operator=(foo& other), should exist, attempt perform this->ri = other.ri, considered incorrect ("ill-formed", reported compiler), since it's re-assignment of reference ri.
finally, suggest see [3], in-depth answer on issue.
Comments
Post a Comment