• Popular Now

• 14
• 12
• 9
• 10
• 13

Archived

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

array of enum's? C++

This topic is 5110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

high, why cant i make an array of enums? instead i have to do: const int WALLS[10] = {15,16,17,18,19,20,21,22,23,24}; why?im guessing its because enums are constants, but obviously i can initialize each element on declaration (with const int), so why not with enums? and is there any trick around it? or am i stuck having half my tiles in enum Tiles{} and WALLS[] outside of this enum? [edited by - graveyard filla on March 23, 2004 6:48:53 PM]

Share on other sites
Can you provide a bit more detail about what you are trying to achieve and what approaches you have tried so far?

Share on other sites
An array of enums:
enum Days { monday, tuesday, wednesday, thursday, friday, saturday, sunday };Days day_array[] = { monday, tuesday, monday, monday };

Share on other sites
si-crane, i dont want an array of enums. i want a enum of arrays. errr, heres what i wanted to do:

enum Tile_Types{ OPEN_SPACE, //0  DOOR, PLAYER, RED_ENEMY, //5 PURPLE_ENEMY, BLUE_ENEMY, YELLOW_ENEMY, WARP_INTERSECTIONS,   CHECK_GO_NORTH, //10		 CHECK_GO_SOUTH,	   FOOD,  GLOWING_FOOD,	  BOMB,//14 WALLS[10]; /// <------- ARRAY OF ENUMS!!!!! OR ENUM ARRAY? //walls would be 15-25? OPEN_CK_NORTH,		 OPEN_CK_SOUTH,  OPEN_WARP,  GREY,			 WHITE		};

now this doesnt work, neither does initializing WALLS[] elements. this is what i had to do

      enum Tile_Types{ OPEN_SPACE, //0  DOOR, PLAYER, RED_ENEMY, //5 PURPLE_ENEMY, BLUE_ENEMY, YELLOW_ENEMY, WARP_INTERSECTIONS,  CHECK_GO_NORTH,  CHECK_GO_SOUTH,	 FOOD,  GLOWING_FOOD,	  BOMB,//14 //now const int walls[10] takes up 15-24 OPEN_CK_NORTH = 25,		 OPEN_CK_SOUTH = 26 OPEN_WARP = 27,  GREY = 28,			 WHITE = 29			};	const int WALLS[10] = {15,16,17,18,19,20,21,22,23,24}; //array of walls

basically i didnt want to write out WALLS1,WALLS2,WALLS3 a million times in my program, so i needed an array of enums. this didnt work, so i had to make a const int WALLS[] outside of my enums. i hope you better understand my problem now. thanks for any help!!

[edited by - graveyard filla on March 23, 2004 7:28:48 PM]

[edited by - graveyard filla on March 23, 2004 7:35:10 PM]

Share on other sites
Well, I don''t know about anyone else, but that doesn''t clarify anything for me. Can you describe what it is you want to achieve without using code samples? Code samples can only describe intent when the code actually works.
In plain english, what do want to do?

Share on other sites
I think I got you graveyard filla. I dont know of a way to use the array in the enum, but youve provided a way yourself (with WALLS outside the enum which should suffice.

Share on other sites
heh, ok well, its hard to describe in english without giving a lot of details but ill try

my pacman clone is near complete. i just finished making the level editor. anyway, up untill now i only had 2 kinds of walls. each kind of tile would be represented in an enum, called Tile_Types. so in my enum Tile_Types() (coded above), i would have

PLAYER,
ENEMY,
WALL1,
WALL2,

well, after i made the map maker, i added a whole bunch more different looking walls. so now, theres 10 types of walls. i didnt want to have to write

PLAYER,
ENEMY,
WALL1,
WALL2,
WALL3,
etc....

so i thought i would make an array inside the enum. therefore doing

PLAYER,
ENEMY,
WALLS[10]

understand? i just needed 10 constants to represent my walls. they had to be distinct numbers, because each wall is a different image. that didnt work, so instead, i left out WALLS[] in my enum. and somewhere else, i did:

const int WALLS[10] = {15,16,17,18,19,20,......};

these numbers start at 15 because in my enum Tile_Types, i get to 14, then i want to start WALLS.

im sorry if this still doesnt make sence. its really not a huge deal, my way works, but it seems hacky and i thought there was a way around this. thanks for any help

Share on other sites
he wants to have an enum of tile types or something. there are 10 different wall types but he doesn''t want to type WALL1, WALL2, etc into his enum definition. personally i don''t see what the big deal is, you only have to type it once. but that''s what he wants. personally i don''t think you can do it, but a question like this tends to lie at the wierd funky edge of the language definition and there may be some obscure hack to get something like this to work.

-me

Share on other sites
Why would you try to use an enum if you don''t want to type the names? That''s the whole point of enums; to give names to values in a type safe way.
Besides, and correct me if I''m wrong, it seems to me that the best you can achieve here is to swap the name WALL4 with WALLS[4], which doesn''t offer any typing advantages, while losing the type safety of using only enums.

Share on other sites
the thing is guys, is that i use these walls in collision detection, and drawing. these enums represent sprites.
so i have to do:

//where map[][] is my array of 32x32 tilesfor(int i = 0; i < 10; i++){if(map[x][y] == WALL[i]) DrawIMG(wall[i],x*32,y*32)}

if i couldnt make an array, i would never be able to do this. i do this drawing in 2 places, AND collision detection works very similaraly. like i said, i would have to be writing out WALL1,WALL2,WALL3 ETC in many places. also, if you look at my example above ^^^ wall[] is my array of wall images (surfaces). so throughout my code i match wall with WALL[i]. so not only would i have to write out WALL1, WALL2, but id id have to write out wall1,wall2, etc. this makes life hell. i hope i cleared things up... but anyway, its all good. thanks guys for the help