OO programming?

Hello Forum!

I've decided to learn c++. I already know other languages like Java and C#.
It's easy to program object orientated with them. C++ seems harder.

I've already read this article (http://www.cplusplus.com/forum/articles/10627/).

My problem right now is:

I have a Terrain.h and Terrain.cpp
I've also got a World.h and World.cpp

Then I instantiate a World class in main.cpp. World's constructor instantiates a Terrain class. The problem is that Terrain needs to know the World.

Just including all header files won't work because of circular inclusion.

Do you have any advises for me?

Thank you!
Last edited on
If forward declarations do not solve the dependency, then you should question whether the design makes sense to begin with.

Perhaps you could show sample code that manifests the "circular inclusions" problem?
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef __WORLD_H__
#define __WORLD_H__

class Player;

#include <unordered_map>
#include "Chunk.h"

using namespace std;

class World : public sf::Drawable, public sf::Transformable {

public:

	static const int WORLD_CHUNKS_WIDTH = 16;
	static const int WORLD_CHUNKS_HEIGHT = 16;
	static const int WORLD_UNITS_WIDTH = Chunk::CHUNK_UNITS_WIDTH * WORLD_CHUNKS_WIDTH;
	static const int WORLD_UNITS_HEIGHT = Chunk::CHUNK_UNITS_HEIGHT * WORLD_CHUNKS_HEIGHT;

	World(Player* player);
	~World();

	void initialize();
	void update(float delta);

	void setTerrainData(sf::Vector2i position, unsigned char id);
	unsigned char getTerrainData(sf::Vector2i position);

	int mod(int a, int b);
	sf::Vector2f getSnappedPostion(sf::Vector2f position);

private:

	Player* player;
	unordered_map<sf::Vector2f, Chunk> chunks;
	unsigned char* terrainData;
	sf::Texture* terrainTexture;

	virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};

#endif 


Chunk.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
#ifndef __CHUNK_H__
#define __CHUNKS_H__

#include <SFML\Graphics.hpp>

class Chunk {

public:

	static const int PIXELS_PER_UNIT = 64;
	static const int CHUNK_UNITS_WIDTH = 16;
	static const int CHUNK_UNITS_HEIGHT = 16;
	static const int CHUNK_PIXELS_WIDTH = CHUNK_UNITS_WIDTH * PIXELS_PER_UNIT;
	static const int CHUNK_PIXELS_HEIGHT = CHUNK_UNITS_HEIGHT * PIXELS_PER_UNIT;

	Chunk(sf::Texture* terrainTexture);
	~Chunk();

	void initialize();

	// I need to call World Methods from here, like getTerrain(x,y) or something.

private:

	sf::VertexArray mesh;

};

#endif 


The Chunk code has a comment where my problem is.

EDIT: And of course I would have a pointer to the world in Chunk
Last edited on
Topic archived. No new replies allowed.