Program Crashes and I can't figure out why. Help please?

Started by
7 comments, last by Radagar 21 years, 9 months ago
I am reading in a simple map from a text file and storing it in a char** MAP variable. so MAP[X][Y] contains the characters for the map. I have a Tile class that I want to store this MAP info in. MAP is just a temporary variable I also have a World class that contains an array of these Tiles. When I try to use a for loop to put all of MAP[X][Y] into world.tile[X][Y].charType, it crashes. Im probably overshooting an array, but I can''t find where. Here is the code. Any help is appreciated... Tile Class
  
class Tile
{
public:
	char tileChar;
};
  
World Class
  
class World
{
public:
	int cameraX;
	int cameraY;
	int mapMaxX;
	int mapMaxY;
	Tile tile[50][50];
};
  
Global Vars
  
char** tilemap;
World world;
  
Now tilemap is holding the MAP[X][Y] from above. This is tested and working fine. world.mapMaxX = 13 world.mapMaxY = 9 Main Function
  
int main()
{
   tilemap = readmap(); //This sets tilemap equal to MAP[X][Y] 

                        //read from the file.


      for (int i = 0; i < world.mapMaxX;i++)
      {
	for  (int j = 0; j < world.mapMaxY;j++)
	{
		world.tile[i][j].tileChar = tilemap[i][j];
	}
      }
   return 0;
}
  
~~~~~~~~~~~ Chris Vogel ~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
Advertisement
In your for loop, when you are referencing the array

world.tile[j].tileChar = tilemap[j];<br><br>try<br><br>world.tile[j].tileChar = tilemap[j]; </i>
Could it be because you have not told C++ that the world class is derived from the tile class?

Define your world class as:


  class World:public Tile{public:     int cameraX;     int cameraY;     int mapMaxX;     int mapMaxY;     Tile tile[50][50];};  


Let us know how it goes!



Regards,
Mathematix.
Thanks for the Suggestions everyone.

switching the world.tile[j] to [j] worked… but I''m not sure why yet. I''ll have to dig into how my MAP[x][y] was returned.<br><br>as for inheriting World from Tile… that wasn''t my original plan. World contains an array of Tile Structures, but World doesn''t use any of the members or functions of Tiles. <br><br>Thanks!<br> </i> <br><br>~~~~~~~~~~~<br>Chris Vogel<br>~~~~~~~~~~~<br>
WyrmSlayer RPG - In Early Development
show the allocation for tilemap
-Warden
Here is my Readmap() Function


  char** readmap(void){	ifstream Input("map1.map"); // Opens the File	char Buffer = NULL; // Buffer to Hold read data	int xCounter = 0; //Calculates how wide the map is	int yCounter = 0; //Calculates how long the map is	bool done = false;	char** MAP; // Holds the Map	while (!done)	{		Input.read(&Buffer, sizeof(char)); // Read in the Char		if (Buffer != ''|'')			xCounter++;		else			done = true;	}	Input.seekg(0,ios::beg);	done = false;	while (!done)	{		Input.read(&Buffer, sizeof(char));		if (Buffer == ''|'')		{			yCounter++;		}		else		{			if(Buffer == ''X'')			{				done = true;				yCounter++;			}		}	}	Input.seekg(0,ios::beg);	MAP = new char*[yCounter];	for(int Sub = 0; Sub < yCounter; Sub++)	{		MAP[Sub] = new char [xCounter];	}	done = false;	for (int Y = 0; Y < yCounter; Y++)	{		for (int X = 0; X < xCounter; X++)		{			Input.read(&Buffer, sizeof(char));			MAP[Y][X] = Buffer;		}		while(Buffer !=''\n'')		{			Input.read(&Buffer,sizeof(char));		}	}	world.mapMaxX = xCounter;	world.mapMaxY = yCounter;	Input.close();	return MAP;  


~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
from what I understand 2d arrays have a lot of complications, especially if you pass them as pointer to pointer, instead of as a 2d array. instead of accessing x,y you need to access x*y+y or something like that. So just use a vector of vectors. The standard library is there for a reason.
I''ve never used vectors before, don''t even know what they are...

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development

MAP[Y][X] = Buffer;


Should this not be X,Y if you wish to use I,J.

,Jay

This topic is closed to new replies.

Advertisement