Advertisement

Newbie question on c++ standard (pointer related)

Started by August 18, 2001 04:09 AM
5 comments, last by Afterlife 23 years, 6 months ago
I have this class (class Maptile) and a worl defined using these maptiles : Maptile *world = create_world(10,10); The create_world() function looks like this :
  Maptile *create_world(short xsize, short ysize)
{
return (new Maptile[xsize*ysize]);
}  
As you can see I''m trying to reserve a 100 maptile classes to a maptile pointer. But obviously I''m doing something wrong. The compiler says about the line, in which the world is defined and the function called : Implict declaration of ''int create_world(...)'' Initialazation to ''Maptile *'' from ''int'' lacks a cast Umm what int? Thanks for any help
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
You'll need to declare your function before you call it.
Either by having the entire definition of the function earlier in the source-file than any calls to it, or be declaring it like this:
        Maptile *create_world(short xsize, short ysize);  


(This declaration must obviously also come before any calls to the function).

The usual way of doing this is to place the declarations of all your functions in a header-file, and including it in the source.

The 'int' is a legacy from way back in early C-days where everything that wasn't specified was implicitly treated as an int. Undeclared functions where regarder to return ints, undeclared variables where treated as ints, and functions that didn't have a specified return-type were assumed to return int's.
(That's why you still see some people declaring their main-functions like this:
   main() {/*  body  */}  


There were a lot of weird ways of doing stuff back then, and if you look at really old C-code (K&R C) you may see something like this:
  Maptile *create_world (xsize, ysize)short xsize;short ysize;{/* Body of function */}  


-Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

Edit: Spelling-mistake.

Edited by - Neophyte on August 18, 2001 7:38:08 AM

Edit: And another one...

Edited by - Neophyte on August 18, 2001 7:39:42 AM

Edit: And yet another one...

Edited by - Neophyte on August 18, 2001 7:40:53 AM
Advertisement
Does the code calling create_world have a function prototype for it? If the calling code is in another file or above create_world in the file, you can't use the create_world identifier. An unknown function defaults to returning int, which might be the issue here. If so, you need a function prototype at the top of your file (eg. Maptile *create_world(short xsize, short ysize)) or you need to put that function above any that call it.

(EDIT: See above post for more details )

Edited by - Kylotan on August 18, 2001 7:47:07 AM
Maptile *create_world (xsize, ysize)
short xsize;short ysize;
{/* Body of function */}
Heh, I rember that

Anyways the function was declared... There must be something else wrong with it. Here's the whole code :

edit : hmm pretty messy without line breaks...

edit 2 : Doh, the function declaration is before the class definition. No wonder it didn't work. Thanks for bringing it to my mind

    #include<iostream.h>#include<time.h>#include<limits.h>#include<stdio.h>#include<conio.h>#include<string.h>#include<allegro.h>#include<math.h>Maptile *create_world(short xsize, short ysize);volatile int millisecs;void count_time(){millisecs++;}END_OF_FUNCTION(count_time);enum TERRAIN_TYPE {empty,grass1,grass2,dirt1,dirt2,water1,water2};class Maptile{public :enum TERRAIN_TYPE terrain;short newx; // Entry to new x,y coordinates (door, teleport, cave...)short newy;unsigned passable : 1;unsigned door : 1;Maptile(){terrain = empty;newx = -1;newy = -1;passable = 0;door = 0;}Maptile(enum TERRAIN_TYPE n_terrain, short n_newx, short n_newy, unsigned short n_passable, unsigned short n_door){terrain = n_terrain;newx = n_newx;newy = n_newy;passable = n_passable;door = n_door;}};BITMAP *buffer;BITMAP *tilegraph[6];int main(){Maptile *world=create_world(10,10);allegro_init();set_color_depth(16);set_gfx_mode(GFX_AUTODETECT,640,480,0,0);install_keyboard();install_timer();install_int(count_time,1);tilegraph[0]=load_bitmap("grass1.pcx",0);tilegraph[1]=load_bitmap("grass2.pcx",0);buffer=create_bitmap(640,480);clear(buffer);allegro_exit();return 0;}Maptile *create_world(short xsize, short ysize){return (new Maptile[xsize*ysize]);}    


Edited by - Afterlife on August 18, 2001 8:22:19 AM

Edited by - Afterlife on August 18, 2001 8:27:18 AM
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
Just out of interest, why use the ''volatile'' qualifier on your milliseconds variable?
quote:
these must be declared volatile so the optimiser doesn't mess up

From Allegro example files. I myself don't even remember what volatile means
I find END_OF_FUNCTION(count_time); even more confusing...

Edited by - Afterlife on August 18, 2001 6:36:42 PM
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
Advertisement
It means that the variable can be changed outside of program control.

~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers

This topic is closed to new replies.

Advertisement