GPWiki.org
GPWiki.org
It is currently Mon May 20, 2013 1:26 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sun Oct 21, 2012 1:26 pm 
Hi,
I would like to make a map (2d array height=8 width=8)
where each element is a class (class Area).
first...in Game_world.h
Code:
class Game_world
{
    public:
    Area *area; // Pointer to class Area.

    void initialise (void); // Initialise class Area in here.
};


I try to initialise class Area.
Code:
Area = new area[8][8]; // Make it like a chess board/map so its simple to work with.


The compiler errors.
I know i could go area = new Area[64] but I find it so much easier on the brain to have it as area[8][8];

Any help as to
1)how i can get round this
2)the technical reason it wont work.

thx in advance.


Top
  
 
PostPosted: Sun Oct 21, 2012 2:12 pm 
Grand Optimizer
User avatar

Joined: Mon Jan 17, 2005 6:01 pm
Posts: 352
Location: Canada
Hi,

Which language/compiler are you using?

What errors are you getting? it could be your syntax.. area = new area[8][8].. you may need to specify that it's an array as your instantiating it.. so area[] or area[,].

If you want to create a 2D array of a class, that's no problem.

If class Area is already properly defined, it's a simple case of:

Map[,] = new Area[8, 8]; //8,8 would actually give you more than 64 elements because arrays start at 0, not 1.. you could try 7,7

then you can freely do things like:

Map[3,5].TileType = 4; // maybe 4 means lava, or whatever.

basically just make sure your area class is properly defined before you go using it. I dont really understand "public: Area *area", so im not sure what to tell you there but hopefully this helps.

_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com


Top
 Profile  
 
PostPosted: Sun Oct 21, 2012 2:33 pm 
visitor wrote:
Hi,
I would like to make a map (2d array height=8 width=8)
where each element is a class (class Area).
first...in Game_world.h
Code:
class Game_world
{
    public:
    Area *area; // Pointer to class Area.

    void initialise (void); // Initialise class Area in here.
};


I try to initialise class Area.
Code:
Area = new area[8][8]; // Make it like a chess board/map so its simple to work with.


The compiler errors.
I know i could go area = new Area[64] but I find it so much easier on the brain to have it as area[8][8];

Any help as to
1)how i can get round this
2)the technical reason it wont work.

thx in advance.


shouldnt it be
Code:
Area **area; // Pointer to class Area.

dont hate on me if its wrong, i'm quite tired and probably not thinking straight:P
(like when you get a char** argv is really a 2 dimensional array of chars)


Top
  
 
PostPosted: Sun Oct 21, 2012 2:51 pm 
Bytewise

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 277
Location: Here (where else?)
Meneliki wrote:
Map[,] = new Area[8, 8]; //8,8 would actually give you more than 64 elements because arrays start at 0, not 1.. you could try 7,7
What C++ book have you been reading?
The comma operator will not do what you think it does. Also, C++ uses exclusive upper-bounds, so 8,8 would be correct if only it actually worked :p

"Area *area" is by definition an array with a single index.
The reason is that C++ does not keep array sizes, so area[i][j] cannot be translated to area[i*Z + j] internally (where Z is the size of the 2nd index).

If you don't mind a fixed size world, the simplest solution is to declare
Quote:
Area area[8][8];
Here you make an array of arrays of Area objects, and "area[i][j]" will give you the right object.

In your "Area = new area[8][8]" you swapped the variable and the type.

If you want it dynamic, you can do something like
Quote:
Area *area;
int sz;

area = new Area[64];
sz = 8;

Area *Get(int i, int j) { return &area[i*sz + j]; }


"Get(3, 5)->height = 18;" would work then. This is also useful when you create parts of the world on demand, that is, the 'Get' could create new areas, or load them from disk or whatever.


Even more dynamic would be
Quote:
Area *rows[];

rows = (Area**)malloc(8*sizeof(Area *)); // The cast may fail, I have not actually tried it.
for (int i = 0; i < 8; i++) { rows[i] = new Area[8]; }
That is, make an array of pointers to an array of objects.

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Sun Oct 21, 2012 3:22 pm 
Yea, Area area[8][8], it needn't be dynamic,I could use that.
Im using c++ and codeblocks ide.

Also the maths you showed using sz=8 variable to get at the right element helps alot.

Thank you very much.


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Exabot [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group