I'm trying to implement a small game where sheeps and wolf roam. My class ground has a vector attribute which contains smart pointers of animal (the abstract class from which sheep and wolf inherit). The full error is :
Error C2280 'std::unique_ptr<animal,std::default_delete>::unique_ptr(const std::unique_ptr<animal,std::default_delete> &)': attempting to reference a deleted function
For the moment, I'm not using wolves, only sheeps. Here is my ground constructor:
ground(SDL_Surface* window_surface_ptr, int n_sheep, int n_wolf) {
window_surface_ptr_ = window_surface_ptr;
nAnimal = n_sheep; //nAnimal is the number of animals in the vector
for (int i = 0; i < nAnimal; i++) {
std::unique_ptr<animal> s(new sheep(window_surface_ptr));
vAnimal.push_back(s); // vAnimal is the vector that contains all the smart pointers
}
};
Here is the application constructor which calls the ground constructor :
application::application(unsigned n_sheep, unsigned n_wolf) : g_(ground(window_surface_ptr_, n_sheep, n_wolf)) {
window_ptr_ = SDL_CreateWindow("SDL2 Window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
frame_width, frame_height,
0);
window_surface_ptr_ = SDL_GetWindowSurface(window_ptr_);
g_.setWindow_surface_ptr_(window_surface_ptr_); //Since g has no default constructor I have to manually re edit its window surface
for (unsigned i = 0; i < g_.getN(); i++) {
g_.setW_(window_surface_ptr_, i); // and edit the window surface of each animal so they appear on the screen
}
}
If you need more of the code (it is quite long so I do not want to bother you with useless one), feel free to ask.
Apparently this error has to do with a copy assignment operator, but I do not know how to fix it, what type to use to define it; is it ground& operator=(const ground&);
or animal& operator=(const ground&)
I think the problem comes from push back wich is equal to vAnimal[n]=s, but since s is a smart pointer it cannot be copied and because I do not have a copy assignment operator the build fails.
EDIT
I have replaced a bit of the code in the ground constructor :
ground(SDL_Surface* window_surface_ptr, int n_sheep, int n_wolf) {
window_surface_ptr_ = window_surface_ptr;
nAnimal = n_sheep; //+ n_wolf;
for (int i = 0; i < nAnimal; i++) {
std::unique_ptr<animal> s = std::make_unique<animal> (sheep(window_surface_ptr));
vAnimal.push_back(s);
}
};
I have also seen someone use vAnimal.push_back(std::move(s)); instead of vAnimal.push_back(s); but that does not change the error :
Error C2280 'std::unique_ptr<animal,std::default_delete>::unique_ptr(const std::unique_ptr<animal,std::default_delete> &)': attempting to reference a deleted function