Invalid 'sizeof' application for incomplete type 'SDL_Window'

Creating a pointer to the SDL_Window structure and assigning it to shared_ptr, the errors mentioned.

Part of the class:

#include <SDL2/SDL.h> class Application { static std::shared_ptr<SDL_Window> window; } 

Definition:

 #include "Application.h" std::shared_ptr<SDL_Window> Application::window{}; bool Application::init() { SDL_Window *window_ = nullptr; if((window_ = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, NULL) ) == nullptr) { std::cerr << "creating window failed: " << SDL_GetError() << std::endl; } window.reset(window_); } 

The error appears in the window.reset () window. What is the reason and how to fix this behavior?

+6
source share
2 answers

By default, shared_ptr free the managed resource with delete . However, if you use a resource that requires release in another way, you will need a custom debugger:

 window.reset(window_, SDL_DestroyWindow); 

NOTE. I am sure this will work, but I do not have SDL installation for testing.

+8
source

As mentioned in Mike , you need to specify your deleter using shared_ptr . However, for unique_ptr you probably want to create a special type for your debiter so that it can be used purely as a template parameter. I used this structure:

 struct SDLWindowDeleter { inline void operator()(SDL_Window* window) { SDL_DestroyWindow(window); } }; 

Then enable the deleter via the template parameter:

 std::unique_ptr<SDL_Window, SDLWindowDeleter> sdlWindowPtr = SDL_CreateWindow(...); 
+7
source

Source: https://habr.com/ru/post/952032/


All Articles