Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Radagar

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

This topic is 5811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ~~~~~~~~~~~

Share this post


Link to post
Share on other sites
Advertisement
In your for loop, when you are referencing the array

world.tile[j].tileChar = tilemap[i][j];

try

world.tile[j][i].tileChar = tilemap[j][i];

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanks for the Suggestions everyone.

switching the world.tile[j] to [j][i] worked... but I''m not sure why yet. I''ll have to dig into how my MAP[x][y] was returned.

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.

Thanks!


~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
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
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
I''ve never used vectors before, don''t even know what they are...

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!