Archived

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

Radagar

Problems with pointers to arrays of objects.

Recommended Posts

Radagar    148
Okay, I have a CTile class holding some info about a map position. I''m declaring the Map as CTile Map[10][10]. Now, I want to pass a pointer to this structure to a function that will draw the map. I can''t get this to work! See my code below, I know I''m doing it wrong, (CMap ** != CMap[10][10]) but, I don''t know how to do it right. Any help would be appreciated.
  
define USE_CONSOLE
#include <allegro.h>

class CTile
{
public:
    BITMAP        *tile_base;
    BITMAP        *tile_terrain;
    bool        passable;
};

PALETTE        palette;
BITMAP        *buffer;
BITMAP        *tile_base_water;
BITMAP        *tile_base_grass;
BITMAP        *tile_terrain_tree;

void draw_map(CTile** pMap);

int main()
{
    allegro_init();
    install_keyboard();

    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);

    //setup Bitmaps

    buffer = create_bitmap(SCREEN_W, SCREEN_H);
    tile_base_water = load_bitmap("water.bmp", palette);
    tile_base_grass = load_bitmap("grass.bmp", palette);
    tile_terrain_tree = load_bitmap("tree.bmp", palette);

    //Create Map

    CTile Map[10][10];

    for (int x = 0; x < 10; x++)
    {
        for (int y = 0; y < 10; y++)
        {
            Map[x][y].passable = true;
            Map[x][y].tile_base = tile_base_grass;
            Map[x][y].tile_terrain = NULL;
        }
    }
    Map[2][5].tile_terrain = tile_terrain_tree;
    //End Map Creation


    while (! key[KEY_ESC] )
    {
        draw_map(Map);
    }
    //Destroy Bitmaps & exit

    destroy_bitmap(buffer);
    destroy_bitmap(tile_base_water);
    destroy_bitmap(tile_base_grass);
    destroy_bitmap(tile_terrain_tree);
    return 0;
}

void draw_map(CTile** pMap)
{
// ...

}
  
Oh, and the error I''m getting if you need it, is
error C2664: ''draw_map'' : cannot convert parameter 1 from ''class CTile [10][10]'' to ''class CTile ** ''
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
 
Thanks! ~~~~~~~~~~~ Chris Vogel ~~~~~~~~~~~

Share this post


Link to post
Share on other sites
I have to say the easiest way would probably be to use a 1 dimensional array instead of 2. If you do this:

CTile Map[100];

You''ll be able to pass a (CTile* pMap) to your function, and bypass your entire problem.

-Arek the Absolute

Share this post


Link to post
Share on other sites
Radagar    148
I really prefer to keep the multi-dimensional array.

I got around my problem by creating a CMap class that holds the CTile array, then just passing a pointer to the map class to the function to draw the screen.

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

Share this post


Link to post
Share on other sites
Glass_Knife    8636
To pass an array to a function, you can do this:
Prototype: void drawMap(CTile pMap[]);
Function call: drawMap(pMap);

The complier does not need to know the size of the array...
but that's for a 1d array, for a 2d array, it need to know the size of the 2nd dimension.

Prototype: void drawMap(CTile pMap[][10]);
Function call: drawMap(pMap);


    
#include <iostream.h>

class FOO{
int x, y;

};

void drawIt(FOO pMap[][10]);

int main()
{
FOO array[10][10];
drawIt(array);

return 0;

}

void drawIt(FOO pMap[][10])
{
cout << " in drawIt" << endl;
}


hope this helps...

I think, therfore I am.
I think?

[edited by - glass_knife on October 2, 2002 6:29:54 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
In addition:

TypeX array[s][x];
TypeX **array2;

''array'' is a constant pointer, where ''array2'' is not.

You could also declare you variable as TypeX** and allocate:

array2 = new TypeX*[10];
for( int i=0; i<10; i++ ){
array2 = new TypeX[10];
}

Now, you can send it to ''func( TypeX** v )''


There may be faster/more efficient ways, but this works.
I (like Glass_Knife) hopes this helps.

---- --- --- -- -- -- -- -- - - -- -
please don''t hesitate to correct any errors. :D
----

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Oops... Sorry, I found a mistake in my code, heh.

The mistake is in the for loop:

array2 = new TypeX[10];

should be:

array2 = new TypeX[10];



:D ~ z ~ :D

Share this post


Link to post
Share on other sites
Hairu    122
It''s simple really. Arrays are pointers just tell the function to use it as a pointer by using the address-of operator like this.

CTile Map[10][10];
void draw_map( CTile** );

..... blah blah blah
....

draw_map( &Map );

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
doesn''t (seem to) work. (use the post before that, or f( ctile x[][10]))

To clean up the allocated array (from my previous code), delete
all ''sub'' arrays first:

for( int i=0; i<0; i++ ){
delete [] array2;
}

delete [] array2;


:D ~z~ :D

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Again! An error.. I need to be hit. heh.

Loop is this:

for( int q=0; q<0; q++ ){
delete [] array2[q];
}


hrm... italics.

Share this post


Link to post
Share on other sites
Hairu    122
This is not correct

Again! An error.. I need to be hit. heh.

// BEGIN BLOCK
Loop is this:

for( int q=0; q<0; q++ ){
delete [] array2[q];
}


hrm... italics.


// END BLOCK

the loop is never accessed, you set q to 0, and then test that it''s less than it.. it''ll never evaluate to true

Share this post


Link to post
Share on other sites
nicho_tedja    122
Try this:

void draw_map(CTile pMap[][]);

...or is it

void draw_map(CTile[][] pMap); ?

I forgot, I never use static array anymore.


My compiler generates one error message: "does not compile."

Share this post


Link to post
Share on other sites
Radagar    148
Thanks for the replies everyone, although I''m still pretty confused =)

It''s working great right now with the CTile array stored in a CMap class, and passing a pointer to that CMap the the draw_screen function.

All is good now.

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

Share this post


Link to post
Share on other sites
dalleboy    324
quote:
Original post by Radagar
I really prefer to keep the multi-dimensional array.

Why not go for a more C++ solution and create your own class for your two-dimensional arrays?

Share this post


Link to post
Share on other sites