unique_ptr can't delete SDL_Window because it's an incomplete type(VC++ Express 2013)

StackOverflow https://stackoverflow.com/questions/22751033

  •  24-06-2023
  •  | 
  •  

Question

I'm trying to use unique_ptr in cpp with SDL_Window, but it's an incomplete type, after I get the first error trying to compile the code, I created my own deleter struct, but it still not working, so here is how I'm trying:

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

and here is the creation of the window:

std::unique_ptr<SDL_Window, SDLWindowDeleter> 
Initializer::create(Uint32 w, Uint32 h, std::string title) {

    std::unique_ptr<SDL_Window, SDLWindowDeleter> window;
    startSDL();

    window = std::unique_ptr<SDL_Window, SDLWindowDeleter>
             (SDL_CreateWindow(title.c_str(), 0, 0, w, h, SDL_WINDOW_OPENGL));

    return window;
}

and to receive the window i use:

window = std::move(Initializer::create(500, 500, "Title"));

the compiler's errors is:

Error   1   error C2027: use of undefined type 'SDL_Window' c:\program files\microsoft visual studio 12.0\vc\include\memory 1198    1   game
Error   2   error C2338: can't delete an incomplete type    c:\program files\microsoft visual studio 12.0\vc\include\memory 1199    1   game
Error   4   error C2679: binary '=' : no operator found which takes a right-hand operand of type 'SDLWindowDeleter' (or there is no acceptable conversion)  c:\program files\microsoft visual studio 12.0\vc\include\memory 1424    1   game

This third error I can't understand why, because if it's a struct, don't need to overload the binary operand '='.

Should I use a "non-safe" pointer? or there exists some way to fix this error?

Was it helpful?

Solution

This is the definition of std::move:

template< class T >
constexpr typename std::remove_reference<T>::type&& move( T&& t );

As the function "Initializer::create" returns the smart pointer "std::unique_ptr", so in your code, std::move will return a reference to the the smart pointer "std::unique_ptr", not to the type "SDLWindowDeleter", that is the reason you get this compiler error.

You should change the "window" variable in the below sentence to type std::unique_ptr<SDL_Window, SDLWindowDeleter> window too.

window = std::move(Initializer::create(500, 500, "Title"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top