Class prototypes, Trouble with more than one include file.

I'm trying to build a small 2D game engine on my own using SDL, I had most of what I wanted semi-working when I had all the classes in one include file but I decided to try separating the class into their own respective header/cpp files. Unfortunately now I have about 162 errors in my code, most of which are dealing with undeclared identifiers and Visual Studio no longer able to differentiate from my function prototypes and my function declarations. Most of the errors say something along the lines of "missing ';' after ')' " where the ')' is the end of my function definition.

Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//In RenderEngine.h

class RenderEngine
{
private:
	GameEngine* Engine;
	static vector<Entity*> EntityList;
	void Display_Entity(Entity* Entity, SDL_Surface* Destination);
	SDL_Surface* Load_Image(std::string Image);
	SDL_Surface *Screen;
	unsigned int Xres;
	unsigned int Yres;

public:
	RenderEngine(GameEngine* _Engine);
	friend class GameEngine;
	Entity* Get_Entity(unsigned int Index);
	bool CreateScreen(unsigned int _Xres, unsigned int _Yres, unsigned int BPP, int flags);
	bool Init(void);
	void Draw(void);
	bool Load_Entity(Entity* Entity);
	bool CleanUp(void);
//says I am missing a ';' here, on this line before the '}'	
};

//In RenderEngine.cpp

SDL_Surface* RenderEngine::Load_Image(std::string Image)//missing ';'
{
	SDL_Surface *LoadedImage = NULL;
	SDL_Surface *OptimizedImage = NULL;

	LoadedImage = IMG_Load(Image.c_str());
	if (LoadedImage != NULL)
	{
		OptimizedImage = SDL_DisplayFormat(LoadedImage);
		SDL_FreeSurface(LoadedImage);
	} //missing ';'
	return OptimizedImage;
}//missing ';'


I think it is because my headers files are all messed up for some reason, each one has a header guard and is only included in one area (The GameEngine.h, which is included in my main.cpp), I don't think the function definitions/prototypes are carrying over from one file to another. Is there any way to do this? Say I have the above class, RenderEngine which contains a vector of type Entity*, and the Entity class contains
friend class RenderEngine is there a way to prototype classes like you can do with functions, so I can make a mention of a class before it is defined?
Obligatory link: http://cplusplus.com/forum/articles/10627/

Specifically see section 4 and up.

Prototyping classes is known as "forward declaring". And yes you can do it.


ANYway I suspect the "missing semicolon" errors are a side-effect of your other errors (undeclared identifiers). Ignore them for now and clean up your other errors. Once you get the other errors cleaned up the semicolon errors will disappear.
I tried placing
1
2
class GameEngine;
class Entity;

In RenderEngine.h and
1
2
class GameEngine;
class RenderEngine;

In Entity.h but it still gives me "undeclared identifier" error.

I'm also getting some "Use of undefined Identifier, see declaration of "RenderEngine" " errors after trying to forward declare some of the classes, when I try to see what it considers to be RenderEngine's declaration it takes me to the "class RenderEngine;". Does the fact that I am trying to use the functions inside each class make a difference to how it has to be declared?

Let me see if I have an understand of the content in that link, instead of including all my files in my main.cpp, I should have one main include file which includes all the other needed files and in the case of a circular dependency I use forward declaring to declare a class outside of its header. Does that mean I have to include the forward declaration in both the header and cpp file? I tried both ways (neither worked)

So if I have my GameEngine.h, which contains the GameEngine class, which contains a pointer to my RenderEngine, FileParser and EventHandler, each of which contains a pointer to a GameEngine class, do I have to forward declare each class inside all the header files?(RenderEngine.h, FileParser.h, EventHandler.h, GameEngine.h)

Last edited on
I should have one main include file which includes all the other needed files


Not really.

Ideally a .cpp file should only #include what it needs. For example, if main.cpp uses GameEngine and Entity, but no other classes, then it should only #include "gameengine.h" and "entity.h".

"package" headers (headers which do nothing but #include common groups of headers) are another option, but overusing them, or making the package too big, defeats much of the point of splitting up headers in the first place. Speaking of, that's one topic I didn't mention in that article.

EDIT: that said.. package headers are often very useful. If you find yourself #including the same 3 header files in every cpp file.. .then yeah you might want to put those 3 headers in a package.


in the case of a circular dependency I use forward declaring to declare a class outside of its header.


You should forward declare IN the header, BEFORE the class, and ONLY for classes that need to be forward declared.

The idea is to make note of the dependencies that each class has, then either forward declare (if possible) or #include the dependency in the header. If all the dependencies are covered, then there's no problem.

So if I have my GameEngine.h, which contains the GameEngine class, which contains a pointer to my RenderEngine, FileParser and EventHandler,


Then those 3 classes should be forward declared in GameEngine.h, before the GameEngine class

each of which contains a pointer to a GameEngine class, do I have to forward declare each class inside all the header files?


If you want to do it the "right way", then yes. GameEngine is a dependency for each of those classes, and thusly must be forward declared.


EDIT 2: If you want, you can post one of your headers in here and I can see if you're doing it right. Try to pick a header that isn't super huge though.

Or you can pastebin it and post the link.
Last edited on
that said.. package headers are often very useful. If you find yourself #including the same 3 header files in every cpp file.. .then yeah you might want to put those 3 headers in a package.
Not to mention the possibility of using precompiled headers.
I *think* it's working now, thank you for the help.

Here a link to GameEngine.h and RenderEngine.h, if you wouldn't mind taking a look at it to make sure that I am doing it the correct way.

GameEngine.h - http://pastebin.com/d21d9401f
RenderEngine.h - http://pastebin.com/d75859a30

EDIT: Now to just fix the access violations :D
Last edited on
#include OR forward declare.

You need to get rid of most of those #includes. That's where your problem is.

let's start with gameengine.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class GameEngine
{
public:
	friend class RenderEngine;
	friend class EventHandler;
	RenderEngine* Render;            // <- forward declared dependency
	EventHandler* EventHandle;    // <- forward
	FileParser* EntParser;            // <- forward
	

	int Start(unsigned int XRes = 800, unsigned int YRes = 600, unsigned int BPP = 32, int flags = SDL_SWSURFACE);
      // Start uses SDL_SWSURFACE, therefore SDL is an #include dependency
	void Run(void);
	
private:
	void Close(void);
	bool IsRunning;
};

// total:  4 depedencies:
//   3 forward delcared
//   1 #include 


therefore your header should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//=====================
//  Forward declared dependencies
class RenderEngine;
class EventHandler;
class FileParser;

//=====================
// include dependencies
#include "SDL.h"

//=====================
// the class
class GameEngine
{
    ...
};


No more... no less.

Apply the same process to RenderEngine. Count the dependencies. Figure out if they're #include or forward declare, and readjust your dependency list accordingly.
Tried removing the uneeded #includes, won't compile. Gives me the same errors as before.

1
2
3
4
error C2227: left of '->Get_ImageFile' must point to class/struct/union/generic type
error C2027: use of undefined type 'Entity'
error C2227: left of '->Image' must point to class/struct/union/generic type
see declaration of 'Entity'

From RenderEngine.cpp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//RenderEngine.h
#ifndef _RENDERENGINE_H_
#define _RENDERENGINE_H_

class GameEngine;
class Entity;

#include "SDL.h"
#include "SDL_image.h" //Needed for an extra library.
#include <string>
#include <vector>

using std::string;
using std::vector;


class RenderEngine
{
private:
	GameEngine* Engine;
	static vector<Entity*> EntityList;
	void Display_Entity(Entity* Entity, SDL_Surface* Destination);
	SDL_Surface* Load_Image(std::string Image);
	SDL_Surface *Screen;
	unsigned int Xres;
	unsigned int Yres;

public:
	RenderEngine(GameEngine* _Engine);
	friend class GameEngine;
	Entity* Get_Entity(unsigned int Index);
	bool CreateScreen(unsigned int _Xres, unsigned int _Yres, unsigned int BPP, int flags);
	bool Init(void);
	void Draw(void);
	bool Load_Entity(Entity* Entity);
	bool CleanUp(void);
	//GameEngine and Entity are the only dependencies, besides SDL.h and SDL_image.h
};

#endif 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//RenderEngine.cpp
#include "RenderEngine.h"

vector<Entity*> RenderEngine::EntityList;

RenderEngine::RenderEngine(GameEngine* _Engine)
{
	Engine = _Engine;
}


SDL_Surface* RenderEngine::Load_Image(std::string Image)
{
	SDL_Surface *LoadedImage = NULL;
	SDL_Surface *OptimizedImage = NULL;

	LoadedImage = IMG_Load(Image.c_str());
	if (LoadedImage != NULL)
	{
		OptimizedImage = SDL_DisplayFormat(LoadedImage);
		SDL_FreeSurface(LoadedImage);
	}
	return OptimizedImage;
}

bool RenderEngine::Load_Entity(Entity* Entity)
{
	Entity->Image = Load_Image(Entity->Get_ImageFile());
	if (Entity->Image == NULL) return false;
	EntityList.push_back(Entity);
	return true;
}

bool RenderEngine::CreateScreen(unsigned int _Xres, unsigned int _Yres, unsigned int BPP, int flags)
{
	if ( (Screen = SDL_SetVideoMode(_Xres, _Yres, BPP, flags)) == NULL ) return false;
	return true;
}

bool RenderEngine::Init()
{
	Screen = NULL;
	return true;
}

void RenderEngine::Draw()
{
	for ( int i = 0; i < EntityList.size(); i++)
	{
		if (EntityList[i]->Depth == 0) Display_Entity(EntityList[i], Screen);
	}
	for (int i = 0; i < EntityList.size(); i++)
	{
		if (EntityList[i]->Depth == 1) Display_Entity(EntityList[i], Screen);
	}
	for (int i = 0; i < EntityList.size(); i++)
	{
		if (EntityList[i]->Depth == 2) Display_Entity(EntityList[i], Screen);
	}
	SDL_Flip(Screen);
}

void RenderEngine::Display_Entity(Entity* Entity, SDL_Surface* Destination)
{
	SDL_Rect offset;
	offset.x = Entity->X;
	offset.y = Entity->Y;

	SDL_BlitSurface(Entity->Image, NULL, Destination, &offset);
}

bool RenderEngine::CleanUp()
{
	for (int i = 0; i < EntityList.size(); i++)
	{
		delete EntityList[i];
	}
	return true;
}

Entity* RenderEngine::Get_Entity(unsigned int Index)
{
	return EntityList[Index];
}


Ignore some of the useless functions(Init, Get_Entity) those are from older builds before I tried putting all the small classes under one big class.

Last edited on
The cpp files should have the #includes

The headers shouldn't. (or at least, the headers shouldn't have more includes than absolutly necessary)

From what I can see RenderEngine.cpp should be including (at least) the following:

RenderEngine.h
SDL_image.h
Entity.h
(possibly more, too tired to check with a fine tooth comb)


Also:

bool RenderEngine::Load_Entity(Entity* Entity)

Ew Ew Ew.

Giving the variable the same name as the type.

I'm dumbfounded that C++ allows that. ew ew ew.
Topic archived. No new replies allowed.