Skip to main content
GameDev.net gamedev.net
🔒 Locked

Trouble w/ forward declarations

Started by kingnosis Dec 27, 2005 at 4:27 PM 3 replies 550+ views
Original Post
kingnosis
kingnosis
So I'm trying to do a simple forward declaration. The idea is to keep all the SDL-specific stuff out of the header file since that will be included all over. This is the basic setup: the header:
struct ScreenData;

class CScreen {
   public:
      CScreen();
      ~CScreen();

   private:
      ScreenData m_Screen;
};
the source:
#include "screen.h"
#include "SDL.h"

struct ScreenData {
   SDL_Surface *surface;
   int x, y;
};
The problem is that I'm getting a "field 'm_Screen' has incomplete type" error from the compiler (MinGW). It's showing the error in the class declaration in the header. There's also a bunch of errors in the source about m_Screen being undefined, but it starts at the header. Any ideas on what I'm doing wrong?
RDragon1
RDragon1
You can't have a member of an incomplete type like that. the definition has to be visible. Remove the forward declaration, #include "ScreenData.h", and remove #include "screen.h" from screendata.h
SiCrane
SiCrane
You can only have pointers or references as member variables to forward declared types. You can't store a forward declared type by value as a member variable.
RDragon1
RDragon1
Oh, if "the source" means that's the corresponding cpp file, well, move ScreenData's definition to where your forward declaration is, and forward declare SDL_Surface instead.
kingnosis
kingnosis
Great. I'm just going to forward-declare the SDL_Surface, since that's a pointer anyway.

Thanks to both.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.