c++ header include to class problem FIXED

Started by
5 comments, last by BaneTrapper 11 years, 8 months ago
/* FIXED */

Hello.
I am not sure how to describe problem, so il trow it at you right now.

At EventHandle.h, i included a "App.h" whyc contains "class App", and i tried to make a object of class App and i get this error
C:\c++ Project\WarCall 0.0.1\EventHandle.h|12|error: 'App' has not been declared| (Line with this error has alot of * simbols)
(i tryed including a "App.h" in .cpp file and making object of class App there, and it works)
ani hints how to bypass this.


/* EventHandle.h */
#ifndef EVENTHANDLE_H_INCLUDED
#define EVENTHANDLE_H_INCLUDED
#include <SDL.h>
#include "App.h"
class EventHandle
{
public:
SDL_Event eventList;
void EventMain(App *obj_App); ************************
void EventKeyDown();
void EventKeyUp();
};
#endif // EVENTHANDLE_H_INCLUDED



/* App.h */
#ifndef APP_H_INCLUDED
#define APP_H_INCLUDED
#include <SDL.h>
#include "define.h"
#include "EventHandle.h"
class App
{
/* Main stuff */
public:
int AppLoop(App *obj_App);
Uint8 Init();
void Exit();
Uint8 AppInit();
void AppExit();
private:
SDL_Surface *surf_Screen;
bool is_On;
public:
SDL_Surface** GETP_surf_Screen();
SDL_Surface* GET_surf_Screen();
void SET_surf_Screen(SDL_Surface*);
bool GET_is_On();
void SET_is_On(bool);
/* Class objects */
private:
EventHandle obj_EventHandle;
};
#endif // APP_H_INCLUDED


/* FIXED */
Advertisement
Remember the #include directive just dumps the contents of that file into whatever includes it. If A includes B and B includes A, then something like this happens. Take a look at Organizing Code Files in C and C++; you'll learn that EventHandle.h doesn't need to include "App.h", but just needs a forward declaration of App.
It looks like App.h includes EventHandle.h, and EventHandle.h includes App.h. This is a problem. It can be solved by *not* including the headers and instead using forward declaration (you can (and should) include the headers in the source files, though). Note that if you use forward declaration, you can only use a pointer to the type.

Wikipedia has a nice example of this (see the "Examples of circular dependency in C++" section especially).

[edit]

ninja'd
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

you'll learn that EventHandle.h doesn't need to include "App.h", but just needs a forward declaration of App.


Well if it was that simple.
I red it all, tryed what i learned, tryed alot of other stuff and im stuck again. Not the best fealing,


I get errors at :
/* EventHandle.h */ Error, forward declaration of struct App
/* EventHandle.cpp */ /* Error here, invalid use of incomplete type 'struct App' */


/* App.h */
#ifndef APP_H_INCLUDED
#define APP_H_INCLUDED
#include <SDL.h>
#include "EventHandle.h"
class App
{
/* Other stuff i have in this class */
/* Class objects */
private:
EventHandle obj_EventHandle;
};
#endif // APP_H_INCLUDED

/* EventHandle.h */
#ifndef EVENTHANDLE_H_INCLUDED
#define EVENTHANDLE_H_INCLUDED
#include <SDL.h>
class App ; /* 'Error, forward declaration of struct App' */
class EventHandle
{
public:
void EventMain(App *obj_App);
};
#endif // EVENTHANDLE_H_INCLUDED

/* EventHandle.cpp */
#include "EventHandle.h"
void EventHandle::EventMain(App *obj_App)
{
while(SDL_PollEvent(&eventList))
{
switch(eventList.type)
{
case SDL_QUIT:
obj_App->SET_is_On(false); /* Error here, invalid use of incomplete type 'struct App' */
break;
}
}
}


Now i cant figure what em i suposed to do. Thats why i included "App.h".

Edit: Do take note, its not my entire code, i cliped out the part i dont think its needed. But i can post it if you require it.

Edit2:
Here is a zipped file of my Project "Warcall 0.0.1"
http://www.mediafire...cqw551i705qrc57
Using codeblock and mingw32 compiler
Maybe you should try to include the App.h in the EventHandle.cpp (before or after including EventHandler.h)? Just a guess thought, but there's clearly no reference to App.h in the code you pasted above.
Regards,
MoroS84
Maybe what I said wasn't very clear. The source files still include all the necessary headers. Look at EventHandle.cpp. It only includes EventHandle.h. And yet, it's trying to use an App object. The compiler can't let you use an object without knowing it's full type. You need to include App.h in EventHandle.cpp in order to allow the compiler to fully understand that App object so it can properly compile your code. It should be:

App.h

#ifndef APP_H_INCLUDED
#define APP_H_INCLUDED

#include "EventHandle.h"

class App
{
private:
EventHandle obj_EventHandle;
};

#endif


EventHandle.h

#ifndef EVENTHANDLE_H_INCLUDED
#define EVENTHANDLE_H_INCLUDED

class App; // Forward declare App

class EventHandle
{
public:
void EventMain(App *obj_App); // The only thing you can do with a forward declaration is declare a pointer
};

#endif


EventHandle.cpp

#include <SDL.h> // I moved SDL.h here because this seems to be the only file that actually needs it

#include "App.h" // Include the App header so that you can actually use an App object
#include "EventHandle.h" // Order of include shouldn't matter, if you have things coded right

void EventHandle::EventMain(App *obj_App)
{
while(SDL_PollEvent(&eventList))
{
switch(eventList.type)
{
case SDL_QUIT:
obj_App->SET_is_On(false); // If you want to actually use the pointer, you must include App.h to get App's full definition
break;
}
}
}


[edit]

I must be blind and didn't see MoroS84's post, or I'm just mega ultra slow and he uber ninja'd me ha...
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Sir Cornstalks,

Bless you . And thank you on your time.


Fixed.

This topic is closed to new replies.

Advertisement