   Question about typedefs
  Hi all, i just recently got back into the game i was writing before i left for a 10 month "obligation", however i am coming across an issue in which i could use some help. For instance i have a typedef enumeration to some character classes (not in the c sense of the word), which i then use for integer arrays containing the charatcer values...like this...typedef enum{/* good guys */CHAR_SOLDIER = 1,////// snip snip //////} unit_t;and then...#define MAX_CHARACTERS 25int AT[MAX_CHARACTERS];//// snip snip /////AT[CHAR_SOLDIER] = 11; <- errors out right herestats.h(30): error C2501: 'AT' : missing storage-class or type specifiersCan you not use enumerations contained in a typedef for an array index?Thanks   
  Why would you want to? This produces the same results:enum unit_t{   CHAR_SOLDIER=1;};EDIT: mispelled soldier.   ;  Sorry to disapoint you but that code compiled fine for me.   ;  Point taken, but it also produces the same error. I also get this:stats.h(30): error C2440: 'initializing' : cannot convert from 'int' to 'int [1]' There are no conversions to array types, although there are conversions to references or pointers to arrays ;Quote:Original post by jsloanstats.h(30): error C2501: 'AT' : missing storage-class or type specifiersCan you not use enumerations contained in a typedef for an array index?ThanksYou definately can use enums for an array index.  I do it often.  However, I don't use the syntax you are using but I don't know if it matters.  Something more like:enum unit_t{/* good guys */CHAR_SOLDIER = 1,////// snip snip //////MAX_CHARACTERS};Notice that I put the MAX_CHARACTERS inside the enum (at the end)?  That way you don't have to remember to update the define when you add more characters (unless you want it to be 25 for a reason).  I would also start your enum at 0 instead of 1 if you are using them for array indexes (unless you're doing that on purpose).But at any rate, I don't think that error has anything to do with your enum usage.  It more sounds like it can't figure out what AT is.  See this description on your error -- maybe that can help you solve it.C2501 erroredit:New error  :P  I'm not sure what your error is for without seeing the code actually being used.   ;  What you are trying to do is commonly called "the enum hack"C++ has another possibility...static const int NUM_CHARACTRERS = 10; // legal syntax in the class definition...but even Stroustrup prefer the enum hack over this method.ref: http://www.research.att.com/~bs/bs_faq2.htmlI think he uses something like this:typedef enum { CHAR_SOLDIER = 1 };#define MAX_CHARACTERS 25...int AT[MAX_CHARACTERS];...AT[CHAR_SOLDIER] = 11;...I made a little test:#include <iostream>class test{    public:    static const int NUM = 10;    enum { ARRAY_INDEX=1 };    int array[NUM];    void f()    {   array[ARRAY_INDEX] = 2;    }};int main(){return 0;}Anyway... try googling for "the enum hack" for more accurate info   ;  jslon,do you have the "AT[CHAR_SOLDIER] = 11;" in a function or at the global file level?it only works in a function, ie.:void InitStats(void){    AT[CHAR_SOLDIER] = 11;};  Ops, sorry, I assumed you where "inside" a class definition :/   ;Quote:Original post by Anonymous Posterjslon,do you have the "AT[CHAR_SOLDIER] = 11;" in a function or at the global file level?it only works in a function, ie.:void InitStats(void){    AT[CHAR_SOLDIER] = 11;}Hi there, i have the declaration of AT in stats.hI have the enums in types.h with stats.h including types.hSo it would be at the global file level i would imagine.I also tried a different compiler, though it did me no good at all.The reason why i have the enum starting out at 1 as opposed to 0 is because i get this errorstats.h(30) : error C2466: cannot allocate an array of constant size 0I dont get it. If anyone wants to see the full context of the code just let me know, something seems jacked here.edit: spelling   ;  like i wrote before:you can NOT do "AT[CHAR_SOLDIER] = 11;" at the global file level,you must put this in a function"stats.h(30) : error C2466: cannot allocate an array of constant size 0"that's because the compiler think you will define a new array with a size of 0so start with 0 in the enum, the 1 is not neededso put all the value settings in a function, ie.:void InitStats(void){AT[CHAR_SOLDIER] = 11;....}(or do:int AT[MAX_CHARACTERS] = {  11,   // CHAR_SOLDIER (start with value 0 not 1 in the enum).....};)i think the function-way is better, because you can simply with a functioncall reset the values and you can also read the values out of a file, so tuning them is easier   