Advertisement

Problems with pointers to arrays of objects.

Started by October 02, 2002 10:41 AM
13 comments, last by Radagar 22 years, 1 month ago
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 ~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
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
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Advertisement
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
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
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]

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

In addition:

TypeX array[x]; <br>TypeX **array2;<br><br>''array'' is a constant pointer, where ''array2'' is not.<br><br>You could also declare you variable as TypeX** and allocate:<br><br>array2 = new TypeX*[10];<br>for( int i=0; i&lt;10; i++ ){<br> array2<i> = new TypeX[10];<br>}<br><br>Now, you can send it to ''func( TypeX** v )''<br><br><br>There may be faster/more efficient ways, but this works. <br>I (like Glass_Knife) hopes this helps. <img src="wink.gif" width=15 height=15 align=middle><br><br>—- — — – – – – – - - – - <br>please don''t hesitate to correct any errors. :D<br>—- </i>
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
Advertisement
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( ⤅ );
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
Again! An error.. I need to be hit. heh.

Loop is this:

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


hrm... italics.
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

This topic is closed to new replies.

Advertisement