5

Although I've seen many questions referring to the "Incomplete type not allowed" error in C++, I still cannot figure out what the compiler is trying to tell me when it screams at me like this. I've been able to piece together that it has something to do with #include-ing header files, but I am clueless as to what an "incomplete type" is and why it is "not allowed". I got the error when trying to inherit from SDL_Window as such:

#pragma once
#include "SDL.h"

class Window : public SDL_Window
{
public:
  Window();
  ~Window();
};

Could someone please explain to me what the error means, how to (generally) fix it, and, in my case, what I should do to stop it from happening?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
tobahhh
  • 369
  • 1
  • 3
  • 10
  • 1
    `#include ` -> `#include "SDL.h"` – George Mar 19 '18 at 01:37
  • Thanks for the suggestion. I changed it, but it didn't fix the error. – tobahhh Mar 19 '18 at 01:39
  • Related: https://stackoverflow.com/questions/18453393/why-sdl-window-is-an-incomplete-type – Ronan Boiteau Mar 19 '18 at 01:39
  • 1
    Perhaps you should try showing the full, complete error message, instead of merely paraphrasing what it says. And providing a [mcve] will not hurt, either. – Sam Varshavchik Mar 19 '18 at 01:42
  • As for the whole error message, all my compiler says is, simply, `incomplete type not allowed`, and the line, `class Window : public SDL_Window`. I do get another error, though, saying `'SDL_Window' base class undefined`. – tobahhh Mar 19 '18 at 01:49
  • 4
    @George `SDL_Window` is incomplete on purpose. It's definition is not visible to consumers of the library, and it can only be interacted with by passing/receiving pointers to it to/from SDL functions. This is a common technique used by many C libraries. – Miles Budnek Mar 19 '18 at 01:50
  • 1
    Because, probably, only de declaration of `SDL_Window` is done in header files and the definition is done elsewhere, where you can not access. – Amadeus Mar 19 '18 at 01:51
  • @MilesBudnek Ah, fair do's. – George Mar 19 '18 at 01:53

1 Answers1

8

C++ - What does “Incomplete type not allowed” error mean

Incomplete type means that there is no definition for the type SDL_Window.

The error means that the type is incomplete, and that an incomplete type wasn't allowed in that context. In this particular case: an incomplete type cannot be used as a base class.

what I should do to stop it from happening?

Do not attempt to use SDL_Window as a base class - it is not intended to be used in that way.

SDL_Window is intended to be used as an opaque pointer. Some SDL functions may return you a SDL_Window*. You can store it, and send it as an argument to other SDL functions. That is all it is used for.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    Ok, thank you. I am used to Java in which almost every program has a class that extends JFrame. However, I suppose that type of syntax isn't do-able in C++. – tobahhh Mar 19 '18 at 01:55
  • 2
    That type of syntax is doable in C++ (for example in Qt you can subclass QFrame); it's SDL that has made the design-decision not to allow it for their API. – Jeremy Friesner Mar 19 '18 at 01:57