memory management - Why can't I reassign a variable that was formerly allocated on the stack in C++ (copy assignment operator of 'Frame' is implicitly deleted)? -


i have following setup:

struct frame {     frame(vector<region> regions_)         : regions(regions_){}     const vector<region> regions; }; 

now, @ different part in code want create vector<frame> , created following for loop:

vector<frame> shot; frame currentframe = generaterandomframe(); (int = 0; < length; i++) {     shot.push_back(currentframe);     currentframe = generatenextframe(currentframe); // line gives error } 

where generatenextframehas following signature: frame framegenerator::generatenextframe(frame previousframe)

now, code won't compile , give me following error:

copy assignment operator of 'frame' implicitly  deleted because field 'regions' has no copy assignment  operator const vector<region> regions; 

now, don't understand error message. assume it's related fact currentframe allocated on stack , not on heap, , can't reassign variable. being novice c++ however, not familiar how handle these kinds of situations. should use pointers here instead , try allocate currentframe on heap?

to clear, goal generate series of frames (that depend on previous frame). can point me right direction here?

update: lot hints in comments, understand issue comes fact declare regions const. rewrote code use pointers instead of heap variables, looks this:

vector<frame> shot; frame currentframe = generaterandomframe(); frame *currentframeptr = &currentframe; // first frame in shot random (int = 0; < length; i++) {     shot.push_back(*currentframeptr);     frame tmpframe = generatenextframe(*currentframeptr);     currentframeptr = &tmpframe;  } 

this code compile now, still doesn't want. according understanding should work, because storing currentframe in pointer, can override new objects create. seems there still bug, frame generation doesn't work expected (that new frame generated 0 regions while number of regions should identical previous frame). can see what's wrong updated version of code?

your struct declares const member, forces compiler implicitly delete default copy assignment operator due fact const member doesn't have copy assignment operator.

the code below replaces bits omitted sample , shows how generate bunch of frame *s instead of frames. basically, it's workaround compilation issue, may not suit specific need if code requires stack usage or you'd have refactor much.

#include <iostream> #include <vector>  struct region{};  struct frame {     frame(std::vector<region> regions_)     : regions(regions_){}     const std::vector<region> regions; };  int main(int argc, const char * argv[]) {      std::vector<frame *> shot;     frame * currentframe = new frame((std::vector<region>()));     (int = 0; < 10; i++) {         shot.push_back(currentframe);         currentframe = new frame(std::vector<region>());     }      return 0; } 

also please note: how use const_cast? - const_cast won't work , cause ub. in case ;)


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -