I have the following setup:
struct Frame {
Frame(vector<Region> regions_)
: regions(regions_){}
const vector<Region> regions;
};
Now, at a different part in my code I want to create a vector<Frame>
and created the following for
loop:
vector<Frame> shot;
Frame currentFrame = generateRandomFrame();
for (int i = 0; i < length; i++) {
shot.push_back(currentFrame);
currentFrame = generateNextFrame(currentFrame); // this line gives the error
}
where generateNextFrame
has the following signature:
Frame FrameGenerator::generateNextFrame(Frame previousFrame)
Now, this code won't compile and give me the following error:
copy assignment operator of 'Frame' is implicitly
deleted because field 'regions' has no copy assignment
operator const vector<Region> regions;
Now, I don't fully understand this error message. I strongly assume that it's related to the fact that currentFrame
is allocated on the stack and not on the heap, and thus I can't just reassign the variable. Being a novice to C++ however, I am not familiar with how to handle these kinds of situations. Should I use pointers here instead and try to allocate currentFrame
on the heap?
To be clear, my goal is to generate a series of frames
(that depend on some previous frame). Can anyone point me into the right direction here?
Update:
Thanks a lot for all the hints in the comments, I now understand that the issue comes from the fact that I declare regions
as const
. I rewrote the code to use pointers instead of heap variables, it now looks like this:
vector<Frame> shot;
Frame currentFrame = generateRandomFrame();
Frame *currentFramePtr = ¤tFrame; // the first frame in the shot is random
for (int i = 0; i < length; i++) {
shot.push_back(*currentFramePtr);
Frame tmpFrame = generateNextFrame(*currentFramePtr);
currentFramePtr = &tmpFrame;
}
This code does compile now, however it still doesn't do what I want. According to my understanding it should now work, because I am storing the currentFrame
in a pointer, so I can easily override it with new objects that I create. But it seems that there still is a bug, the frame generation doesn't work as expected (that is any new frame is generated with 0 regions while the number of regions should be identical to the previous frame). Can anyone see what's wrong with this updated version of my code?