Inheritence Problem

I am making a little prototype for an editor and I came a across a build problem with my classes...
"Syntax Error, need ; before *"
and "Map undefined as base class"

What am I doing wrong?

tile.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
#ifndef Tile_h
#define Tile_h
//#include "character.h"
#include "map.h"
#include <iostream>

#define TILE 0
#define EVENT 1
#define MASK 2
#define CHARACTER 3
#define OVERLAY 4
#define COLLISION 5

using namespace std;

class TileLocation:public Map
{
protected:
	string file;
	int type;
	char tile;
	int id;
public: 
	TileLocation(int i);
	//TileLocation(string file, int i, int t, int x, int y);
	//void action();
//	void overlay(Character &c);
	//~TileLocation();
};
#endif 


map.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

#ifndef Map_h
#define Map_h
#include "tile.h"
#include <iostream>
#include <string>

using namespace std;

//keep in mind, because of the nature of the 2d array, we will keep the format at [y][x].
class Map
{
	protected:
		TileLocation ***world;
		int y;
		int x;
		int z; //oh my god overlays
		int mapSize_x; //columns
		int mapSize_y; //rows
		string background;
	public:
		Map();
		Map(int xSize, int ySize);
		void setUpMap();
		void drawMap();
		void loadMap(string fileName);
		void saveMap(string fileName);
		~Map();
};
#endif 
Last edited on
tile.h line 16

1
2
3
class TileLocation:public Map
{
protected:


or line 14 map.h

You seem to be creating a new TileLocation here and you are specifying what looks like a pointer to a pointer to a pointer ??? 0_0. Anyway, I'm not sure if that might be the problem, but if after fixing line 16, you still get an error, try creating the actual object in there.

So TileLocation ***world = new **TileLocation[];

IMO Tile should not inherit from Map, rather Map could have an array of Tile.

This is the "HAS A " relationship, which IMO would be better than the "IS A" relation you have with inheritance. A Tile is not a Map.

HTH
Alright, I've changed it so TileLocation does not inherit from the Map class, now I still have the C2143 (; missing) error in line 14 of map.h
However, I do not find a problem in this code. Can you help me?
Oh and also another error C4430 (int assumed) for the same line.
I tried to compile your two header files. What I noticed is that you have circular includes. Depending on the order of the includes in main.cpp, I got different errors.

What I did was:
1
2
3
 
#include "tile.h"
#include "map.h" 


When I removed the #include "map.h" from tile.h and the #include "tile.h" from map.h, I was able to compile without error.


Maybe you should try making a 2d array (with constant sizes) of Tiles in the Map, rather than 3 layers of pointers. Why do you want 3d any way? It might be a nightmare to manage, and it is better to start with something simple first, get it working, then move to more complexity later.

One way to do things, is to have a CRoom class which has a 2d array of CTile objects, then have a CGameMap class which has a std::list of CRoom objects. The user moves from one room to the next in a linear fashion.

With the names, I would just have Tile instead of TileLocation as the location is stored in the map. The Tile class only stored info about the tile itself. It is also a good idea to make the file names the same as the class names, which should happen automatically if you use a class wizard in your IDE.

I put a leading C on my class names (e.g. CTile), and a leading p on pointer variables - but that is just me (some people hate doing this). Also, I start member variables with m_ , and use CamelCase rather than underscores (e.g. m_MapSizeX), this makes it easier to name arguments (e.g. MapSizeX). These last two are much common conventions. Too many underscores make the variable names too long IMO.

I used to have protected member variables - (why do you want to inherit from Map any way?) , but I have recently learnt that it is better to make them all private, and provide functions that form an interface to the class.

Finally, try to avoid using namespace std; it brings the whole std namespace into the global namespace, polluting it and causing naming conflicts. Did you know that std::map is an STL data container?
Instead, either put std:: before each std thing (std::string say), or do this :

1
2
3
using std::cin;
using std::cout;
using sdtd::endl;


I do a mixture - have using statements for things that appear frequently, and put std:: for things which only appear a couple of times.

HTH
Topic archived. No new replies allowed.