Jump to content
  • Advertisement

Archived

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

apatequil

dumb array problem

This topic is 5418 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

Hi guys, i''m having trouble with array initializing. here''s the background: I''m writing a tile-based game and need a map loader. I''ve created a tile class that hold the coordinates of the tile (i''ll add other stuff later). For my map, i''m creating a 2d array. This is where my problem starts. I don''t want to be limited to a specific size map, so in the format that i''m storing my map i have a header that says what size the map should be (width by height). This is all fine and everything, except when I actually try to compile the code i get the following error: non-constant expression as array bound Any ideas on how to get past this? Here''s the actual code:
#include <fstream.h>
#include <iostream.h>
#include "main.h"
#include "Tile.h"

class Map {

public:

	mapHeader header;
	Tile  *matrix;
	char filename;

	// Our map constructor

	Map();
	
	// Function to load/save map data

	void loadMap(char *);
	void saveMap(char *);

};


Map::Map()
{
}

void Map::loadMap(char * fileName)
{
	ifstream fin(fileName, ios::binary);
	if(!fin.good())
		MessageBox(NULL, "file didn''t open", "Error", MB_OK);
	else
	{
		fin.read((char *)(&header), sizeof(header));
		matrix = new Tile[header.xSize][header.ySize];
	}
}

void Map::saveMap(char * fileName)
{
	ofstream fout(fileName, ios::binary);
	fout.write((char *)(&header), sizeof(header));
	fout.write((char *)(&matrix), sizeof(matrix));
	fout.close();
}

Share this post


Link to post
Share on other sites
Advertisement
You cannot allocate a dual dimension array in a single call. Change

matrix = new Tile[header.xSize][header.ySize];

to


matrix = new Tile*[header.xSize];
for ( int i = 0; i < header.xSize; i++ )
matrix[i] = new Tile[header.ySize];


Or something like that. I may have the syntax slightly wrong (I'm really tired and I have not compiled it to check) :-)

[edited by - mauman on August 18, 2003 12:57:22 AM]

[edited by - mauman on August 18, 2003 12:58:27 AM]

Share this post


Link to post
Share on other sites
Not a problem. Note that I changed the source tags because my matrix sub i was being interpreted as an italics command :-)

Share this post


Link to post
Share on other sites
hmmm.....i'm getting two errors now:

error C2440: '=' : cannot convert from 'class Tile ** ' to 'class Tile *'

on the line:
matrix = new Tile*[header.xSize];


error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Tile *' (or there is no acceptable conversion)

on the line:
matrix[i] = new Tile[header.ySize];


any ideas?

here's the full code with the modifications:
   
#include <fstream.h>
#include <iostream.h>
#include "main.h"
#include "Tile.h"

class Map {

public:

mapHeader header;
Tile *matrix;
char filename;

// Our map constructor

Map();

// Function to load/save map data

void loadMap(char *);
void saveMap(char *);

};


Map::Map()
{
}

void Map::loadMap(char * fileName)
{
ifstream fin(fileName, ios::binary);
if(!fin.good())
MessageBox(NULL, "file didn't open", "Error", MB_OK);
else
{
fin.read((char *)(&header), sizeof(header));
matrix = new Tile*[header.xSize];
for ( int i = 0; i < header.xSize; i++ )
matrix[i] = new Tile[header.ySize];
}
}

void Map::saveMap(char * fileName)
{
ofstream fout(fileName, ios::binary);
fout.write((char *)(&header), sizeof(header));
fout.write((char *)(&matrix), sizeof(matrix));
fout.close();
}




[edited by - apatequil on August 19, 2003 1:19:49 AM]

Share this post


Link to post
Share on other sites
Since it's a dual dimension array matrix needs to be declared with two levels of indirection (** instead of *):


Tile** matrix;




[edited by - mauman on August 19, 2003 1:29:11 AM]

Share this post


Link to post
Share on other sites
Just curious.. what would be the CORRECT way to clean up Matrix? I was having a similar problem, but was worried about memory leaks, so I left the idea behind.



- Moquan

Share this post


Link to post
Share on other sites
hmmm...that''s a good question. i hadn''t thought about that yet.
i come from more of a java background, so I didn''t really need to worry about that until now. Would a loop with deletes work? after looping through, delete the matrix itself? i suppose there''s a logic flaw in that since it''s the first thing I thought of (which I''ve found to be wrong 99% of the time).

Share this post


Link to post
Share on other sites

Tile **matrix;

//do whatever you do with it...


//---Clean it up---


//delete stuff each pointer points to


for (int i = 0; i < NUM_OF_ITEMS_IN_MATRIX; i++)
delete matrix[i]; //delete the Tile object in memory


//then delete the actual array of pointers

delete [] matrix;

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!