Problem with classes and #includes again...

Hi! I had a really similar quesiton two days ago, solved it my using guard macros.

I have two classes defined in their header files, "Polygon" and "World", one needs the other, so they must #include each other, thats ok if i put the #include below the guard macro.

But for some reason now it wont compile, if I want to initialize a variable of one class in the other class, the compiler gives me a syntax error for using that identifier.

The solution is probably kinda easy again, but even after trying some random stuff nothing seemed to help, only solution would be to put both classes in one file...

That are the headerfiles of both classes:

"World.h":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef WORLD_H
#define WORLD_H

#include "Polygon.h"

class World
{
public:
	World();
	World(int);
	~World();

	void WorldLoop();
	bool AddPolygonToWorld(Polygon *);

	bool active;

	int max_polygon_number;
	Polygon * polygon_list;

	VEC light_dir;
};

#endif 


"Polygon.h":

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
#ifndef POLYGON_H
#define POLYGON_H

#include "mathematics.h"
#include "World.h"

class Polygon
{
private:
	void init(const Polygon&);
	void FrameJumpPosition();
	void FrameJumpAngular();
	void UpdateEdgesAndSurfaces();
	void UpdateAngularVelocity();
	void InitializeLoop();
	void ObjectCollision();
	void GroundCollision();
	bool GroundCollisionCheck(int);
	MAT GetMomentumOfInertia();
	MAT GetInversedMomentumOfInertia();

	World * world;

	VEC * normalized_vertices;
	MAT ang_vel_mat;
	VEC ang_vel;
	VEC ang_mom;
	MAT normalized_momentum_of_inertia;
public:
	Polygon();
	Polygon(int, int, int);
	Polygon(const Polygon&);
	Polygon& operator = (const Polygon&);
	~Polygon();

	void InitializePrivateData();
	void InitializeMomentumOfInertia(MAT);
	void GiveAddressOfStructData(VEC*, int*, int*);
	void SetAngularMomentum(VEC);
	void AddAngularMomentum(VEC);
	void PolygonLoop();
	void Impact(VEC, VEC);
	void ImpactLocal(VEC, VEC);

	bool active;
	int number_vertices;
	int number_edges;
	int number_surfaces;
	VEC pos;
	VEC vel;
	MAT ang;
	VEC * vertices;
	int * edges;
	int * surfaces;
	VEC * surfaces_dir;
	float * color;
	double mass;
	float ELASTICITY;

	bool collision_calc_done;

	long frame_counter = 0;
};

#endif 


In first file the line
 
bool AddPolygonToWorld(Polygon *);

and in second file the line
 
World * world;


won't compile.

Thank you!
A classic case of recursive includes.

A forward class declaration works as long as the second file does not need a fully specified declaration of World. This is the case since line 22 is a pointer and the type of a pointer does not need to be fully specified.

polygon.h
------------
Line 5: Remove

Line 6:
 
class World;  // Forward declaration 
Last edited on
well, thank you very much!
as i expected an easy solution... :)
Last edited on
Topic archived. No new replies allowed.