Incomplete type 'SDL_Window' used in type trait expression?

Hi,

I’m doing a small ‘app’ class that will handle the SDL_Window and SDL_Renderer objects, but I can’t compile. I have tried to do forward declaration, move include decl around but no use.

Here is the reduced code snippet:

Code:

// App.h
#include "SDL2/SDL.h"
using namespace std;

namespace igf {
class App{
public:
static shared_ptr Instance();
shared_ptr<SDL_Window> window;
void Clear();
void Present();
void Cleanup();
int Init();
void UpdateEvents();
bool Quit();
protected:
App();
SDL_Event event;
bool quit = false;
private:
static shared_ptr instance;
};
}

// App.cpp
#include "App.h"
namespace igf {
// Implemented functions.
int App::Init()
{
// some code here …

    // Offending line
    window = make_shared<SDL_Window>(SDL_CreateWindow("SDL 2 window",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,1024,768,SDL_WINDOW_OPENGL),SDL_DestroyWindow);

    //more code here .. etc
    return 0;
}

//more implemented functions   

}

I’m declaring the header file and I’m not using circular references. I don’t understand why it gives me that error.

I had this working with regular pointers. But I want to use smart pointers for this. Any ideas?

Thanks!

I guess it fails to compile here:

Code:
window = make_shared<SDL_Window>(SDL_CreateWindow(“SDL 2 window”,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,1024,768,SDL_WINDOW_OPENGL),SDL_DestroyWindow);

First, have you enabled C++11 support on your compiler ?

Second, the function make_shared() takes parameter which would be passed to the type’s constructor. In this case you passed SDL_DestroyWindow() which returns void. That does not qualify as a constructor’s parameter. (reference : http://www.cplusplus.com/reference/memory/make_shared/)

Third, there is no SDL_Window’s constructor nor destructor. Calling make_shared() would failed because there’s no constructor. Even if it success to create, when the pointer is destroyed, the ‘delete’ function would simply make things even worse (crash, perhaps).

In this case you have to use the normal shared_ptr’s constructor that take ‘deleter’ as a parameter. Something like this (using lambda exp. I have not tested so it might not even compile:-).

Code:
window =shared_ptr<SDL_Window>(
SDL_CreateWindow(“SDL 2 window”,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,1024,768,SDL_WINDOW_OPENGL),
[](SDL_Window* wind){SDL_DestroyWindow(wind);});

One more thing. I think it’s not really wise to call using namespace xxxx even if it’s std. You don’t want to surprise any user of your code including yourself :).

Thank you very much! It worked :slight_smile:

I’m just starting to learn C++ (Learning from C++ Primer), need to pay more attention to the details (void function for the destructor). As for lambdas, I’m not there yet. I will follow your advice on avoiding ‘using namespace’, you’re right. It’s a bad practice [Embarassed]

P.S. It actually compiled as it was :wink:

I’d suggest to get a copy of ‘The C++ Programming Language’ by Bjarne Stroustrup after you finnished reading C++ Primer :). You’ll learn a lot from the master himself !

Enjoy coding!!

I have that book already! I bought them together. Reading it after C++ Primer is the plan :smiley: