Enum list as array index problem

Discussion in 'C++' started by dof, Nov 5, 2005.

  1. dof

    dof Guest

    I'm trying the following and having problems. I get errors on the array declaration lines. Something about an array must have at least one element. Thanks in advance. D

    #include stuff
    ....

    const enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};

    RWCString MESSAGES[ENUM_LIST_MAX]; <---------------------

    MESSAGES[A] = "MESSAGE_A";
    MESSAGES = "MESSAGE_B";
    ....
    MESSAGES{ENUM_LIST_MAX] = "";


    unsigned int CODES[ENUM_LIST_MAX]; <-----------------------

    CODES[A] = 0x0010;
    CODES = 0x000F;
    ....
    CODES[E] = 0x1000;
    CODES[ENUM_LIST_MAX] = 0x0000;

    }
     
    dof, Nov 5, 2005
    #1
    1. Advertising

  2. dof

    David Hilsee Guest

    >"dof" <> wrote in message
    news:...
    >I'm trying the following and having problems. I get errors on the array

    declaration lines. Something >about an array must have at least one element.
    Thanks in advance. D

    >#include stuff
    >...
    >
    >const enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};


    Not sure what that const is doing there.

    >
    > RWCString MESSAGES[ENUM_LIST_MAX]; <---------------------
    >
    > MESSAGES[A] = "MESSAGE_A";
    > MESSAGES = "MESSAGE_B";
    >...
    > MESSAGES{ENUM_LIST_MAX] = "";


    Ignoring the { which should be a [, ENUM_LIST_MAX is not a valid index. The
    array is of size ENUM_LIST_MAX, with valid indexes ranging from 0 to
    ENUM_LIST_MAX-1, inclusive.

    > unsigned int CODES[ENUM_LIST_MAX]; <-----------------------
    >
    > CODES[A] = 0x0010;
    > CODES = 0x000F;
    >...
    > CODES[E] = 0x1000;
    > CODES[ENUM_LIST_MAX] = 0x0000;


    Same problem here: invalid index.

    >
    >}


    You should post complete code that demonstrates this "something about an
    array must have at least one element" problem. It would be best if you
    copied and pasted the code and the exact message emitted by your compiler.

    --
    David Hilsee
     
    David Hilsee, Nov 5, 2005
    #2
    1. Advertising

  3. dof

    dof Guest

    Thanks David.

    You're right about the two lines you mentioned. However, they are not
    causing the error. The code listed is representative of the code that's
    causing the problem. Unfortunately, I can't copy it exactly.

    The error message is as indicated. The compiler crumps at the lines
    indicated saying that the array requires at least one element. A specific
    question would be

    Can I declare an enum, create an unitialized array using that enum as an
    index, and then individually set the elements of the array? If it weren't
    for an ordering issue, I'd initialize the variables inline.

    The following code is in types.h and gets included in other class' .h/.C
    files.

    #include stuff;

    enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};

    RWCString MESSAGES[ENUM_LIST_MAX]; <---------------------

    MESSAGES[A] = "MESSAGE_A";
    MESSAGES = "MESSAGE_B";
    ....
    MESSAGES[E] = "MESSAGE_E";


    unsigned int CODES[ENUM_LIST_MAX]; <-----------------------

    CODES[A] = 0x0010;
    CODES = 0x000F;
    ....
    CODES[E] = 0x1000;
    }

    Thanks,

    D
    "David Hilsee" <> wrote in message
    news:...
    > >"dof" <> wrote in message

    > news:...
    >>I'm trying the following and having problems. I get errors on the array

    > declaration lines. Something >about an array must have at least one
    > element.
    > Thanks in advance. D
    >
    >>#include stuff
    >>...
    >>
    >>const enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};

    >
    > Not sure what that const is doing there.
    >
    >>
    >> RWCString MESSAGES[ENUM_LIST_MAX]; <---------------------
    >>
    >> MESSAGES[A] = "MESSAGE_A";
    >> MESSAGES = "MESSAGE_B";
    >>...
    >> MESSAGES{ENUM_LIST_MAX] = "";

    >
    > Ignoring the { which should be a [, ENUM_LIST_MAX is not a valid index.
    > The
    > array is of size ENUM_LIST_MAX, with valid indexes ranging from 0 to
    > ENUM_LIST_MAX-1, inclusive.
    >
    >> unsigned int CODES[ENUM_LIST_MAX]; <-----------------------
    >>
    >> CODES[A] = 0x0010;
    >> CODES = 0x000F;
    >>...
    >> CODES[E] = 0x1000;
    >> CODES[ENUM_LIST_MAX] = 0x0000;

    >
    > Same problem here: invalid index.
    >
    >>
    >>}

    >
    > You should post complete code that demonstrates this "something about an
    > array must have at least one element" problem. It would be best if you
    > copied and pasted the code and the exact message emitted by your compiler.
    >
    > --
    > David Hilsee
    >
    >
     
    dof, Nov 5, 2005
    #3
  4. dof

    dof Guest

    Thanks David.

    You're right about the two lines you mentioned. However, they are not
    causing the error. The code listed is representative of the code that's
    causing the problem. Unfortunately, I can't copy it exactly.

    The error message is as indicated. The compiler crumps at the lines
    indicated saying that the array requires at least one element. A specific
    question would be

    Can I declare an enum, create an unitialized array using that enum as an
    index, and then individually set the elements of the array? If it weren't
    for an ordering issue, I'd initialize the variables inline.

    The following code is in types.h and gets included in other class' .h/.C
    files.

    #include stuff;

    enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};

    RWCString MESSAGES[ENUM_LIST_MAX]; <---------------------

    MESSAGES[A] = "MESSAGE_A";
    MESSAGES = "MESSAGE_B";
    ....
    MESSAGES[E] = "MESSAGE_E";


    unsigned int CODES[ENUM_LIST_MAX]; <-----------------------

    CODES[A] = 0x0010;
    CODES = 0x000F;
    ....
    CODES[E] = 0x1000;
    }

    Thanks,

    D
    "David Hilsee" <> wrote in message
    news:...
    > >"dof" <> wrote in message

    > news:...
    >>I'm trying the following and having problems. I get errors on the array

    > declaration lines. Something >about an array must have at least one
    > element.
    > Thanks in advance. D
    >
    >>#include stuff
    >>...
    >>
    >>const enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};

    >
    > Not sure what that const is doing there.
    >
    >>
    >> RWCString MESSAGES[ENUM_LIST_MAX]; <---------------------
    >>
    >> MESSAGES[A] = "MESSAGE_A";
    >> MESSAGES = "MESSAGE_B";
    >>...
    >> MESSAGES{ENUM_LIST_MAX] = "";

    >
    > Ignoring the { which should be a [, ENUM_LIST_MAX is not a valid index.
    > The
    > array is of size ENUM_LIST_MAX, with valid indexes ranging from 0 to
    > ENUM_LIST_MAX-1, inclusive.
    >
    >> unsigned int CODES[ENUM_LIST_MAX]; <-----------------------
    >>
    >> CODES[A] = 0x0010;
    >> CODES = 0x000F;
    >>...
    >> CODES[E] = 0x1000;
    >> CODES[ENUM_LIST_MAX] = 0x0000;

    >
    > Same problem here: invalid index.
    >
    >>
    >>}

    >
    > You should post complete code that demonstrates this "something about an
    > array must have at least one element" problem. It would be best if you
    > copied and pasted the code and the exact message emitted by your compiler.
    >
    > --
    > David Hilsee
    >
    >
     
    dof, Nov 5, 2005
    #4
  5. dof

    red floyd Guest

    dof wrote:
    > [redacted]
    >


    Just out of curiosity, is there a reason that your code is written in
    late '60s/early '70s FORTRAN style?

    There's a reason that God invented lower case.
     
    red floyd, Nov 5, 2005
    #5
  6. dof

    dof Guest

    LOL. Our coding standards require constants in caps.

    D

    "red floyd" <> wrote in message
    news:FYYaf.9101$...
    > dof wrote:
    >> [redacted]
    >>

    >
    > Just out of curiosity, is there a reason that your code is written in late
    > '60s/early '70s FORTRAN style?
    >
    > There's a reason that God invented lower case.
     
    dof, Nov 5, 2005
    #6
  7. dof

    David Hilsee Guest

    "dof" <> wrote in message
    news:...
    > Thanks David.
    >
    > You're right about the two lines you mentioned. However, they are not
    > causing the error. The code listed is representative of the code that's
    > causing the problem. Unfortunately, I can't copy it exactly.
    >
    > The error message is as indicated. The compiler crumps at the lines
    > indicated saying that the array requires at least one element. A specific
    > question would be
    >
    > Can I declare an enum, create an unitialized array using that enum as an
    > index, and then individually set the elements of the array? If it weren't
    > for an ordering issue, I'd initialize the variables inline.
    >
    > The following code is in types.h and gets included in other class' .h/.C
    > files.
    >
    > #include stuff;
    >
    > enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};
    >
    > RWCString MESSAGES[ENUM_LIST_MAX]; <---------------------
    >
    > MESSAGES[A] = "MESSAGE_A";
    > MESSAGES = "MESSAGE_B";
    > ...
    > MESSAGES[E] = "MESSAGE_E";
    >
    >
    > unsigned int CODES[ENUM_LIST_MAX]; <-----------------------
    >
    > CODES[A] = 0x0010;
    > CODES = 0x000F;
    > ...
    > CODES[E] = 0x1000;
    > }


    I don't see anything wrong with what you provided above, but I can't see the
    whole picture. The following compiles just fine with MSVC++2003 and with
    Comeau's compiler.

    #include <string>

    enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};

    int main () {
    std::string MESSAGES[ENUM_LIST_MAX];
    MESSAGES[A] = "foo";
    MESSAGES = "bar";

    unsigned int CODES[ENUM_LIST_MAX];

    CODES[A] = 0x0010;
    CODES = 0x000F;
    }

    I suspect that you are omitting something important.

    --
    David Hilsee
     
    David Hilsee, Nov 5, 2005
    #7
  8. dof

    dof Guest

    Hi David,

    You're initializing the messages in main(). I'm trying to initialize them
    inline in the header. I want them static since I have several classes that
    include the header file containing those definitions. Maybe that's the
    problem?

    D

    "David Hilsee" <> wrote in message
    news:...
    > "dof" <> wrote in message
    > news:...
    >> Thanks David.
    >>
    >> You're right about the two lines you mentioned. However, they are not
    >> causing the error. The code listed is representative of the code that's
    >> causing the problem. Unfortunately, I can't copy it exactly.
    >>
    >> The error message is as indicated. The compiler crumps at the lines
    >> indicated saying that the array requires at least one element. A specific
    >> question would be
    >>
    >> Can I declare an enum, create an unitialized array using that enum as an
    >> index, and then individually set the elements of the array? If it weren't
    >> for an ordering issue, I'd initialize the variables inline.
    >>
    >> The following code is in types.h and gets included in other class' .h/.C
    >> files.
    >>
    >> #include stuff;
    >>
    >> enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};
    >>
    >> RWCString MESSAGES[ENUM_LIST_MAX]; <---------------------
    >>
    >> MESSAGES[A] = "MESSAGE_A";
    >> MESSAGES = "MESSAGE_B";
    >> ...
    >> MESSAGES[E] = "MESSAGE_E";
    >>
    >>
    >> unsigned int CODES[ENUM_LIST_MAX]; <-----------------------
    >>
    >> CODES[A] = 0x0010;
    >> CODES = 0x000F;
    >> ...
    >> CODES[E] = 0x1000;
    >> }

    >
    > I don't see anything wrong with what you provided above, but I can't see
    > the
    > whole picture. The following compiles just fine with MSVC++2003 and with
    > Comeau's compiler.
    >
    > #include <string>
    >
    > enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};
    >
    > int main () {
    > std::string MESSAGES[ENUM_LIST_MAX];
    > MESSAGES[A] = "foo";
    > MESSAGES = "bar";
    >
    > unsigned int CODES[ENUM_LIST_MAX];
    >
    > CODES[A] = 0x0010;
    > CODES = 0x000F;
    > }
    >
    > I suspect that you are omitting something important.
    >
    > --
    > David Hilsee
    >
    >
     
    dof, Nov 5, 2005
    #8
  9. dof

    David Hilsee Guest

    "dof" <> wrote in message
    news:...
    > Hi David,
    >
    > You're initializing the messages in main(). I'm trying to initialize them
    > inline in the header. I want them static since I have several classes that
    > include the header file containing those definitions. Maybe that's the
    > problem?

    [...]

    Could be. If you want to share an array amongst multiple implementation
    files, you should declare it in a header and define it separately in one
    implementation file. Example:

    //----- begin file test.h -------
    #ifndef TEST_H
    #define TEST_H
    #include <string>

    enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};

    extern std::string MESSAGES[ENUM_LIST_MAX];

    extern unsigned int CODES[ENUM_LIST_MAX];

    #endif
    //----- end file test.h -------

    //----- begin file test.cpp -------
    #include "test.h"

    std::string MESSAGES[ENUM_LIST_MAX] = {"foo","far","BAZ","etc"};

    unsigned int CODES[ENUM_LIST_MAX];

    bool fillCodes() {
    CODES[A] = 0x0f;
    //etc
    return false;
    }

    // alternate way to populate the array: call a function
    bool dummy = fillCodes();
    //----- end file test.cpp -------

    //----- begin file main.cpp -------
    #include "test.h"
    #include <iostream>

    int main () {
    std::cout << CODES[A] << " " << MESSAGES[A] << std::endl;
    }
    //----- end file main.cpp -------

    HTH.

    --
    David Hilsee
     
    David Hilsee, Nov 5, 2005
    #9
  10. dof

    Neelesh Guest

    dof wrote:

    > Hi David,
    >
    > You're initializing the messages in main(). I'm trying to initialize them
    > inline in the header. I want them static since I have several classes that
    > include the header file containing those definitions. Maybe that's the
    > problem?
    >
    > >> enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};
    > >>
    > >> RWCString MESSAGES[ENUM_LIST_MAX]; <---------------------
    > >>
    > >> MESSAGES[A] = "MESSAGE_A";
    > >> MESSAGES = "MESSAGE_B";
    > >> ...
    > >> MESSAGES[E] = "MESSAGE_E";


    dof wrote:
    > Hi David,
    >
    > You're initializing the messages in main(). I'm trying to initialize them
    > inline in the header. I want them static since I have several classes that
    > include the header file containing those definitions. Maybe that's the
    > problem?


    You cannot write general statements in a global-scope outside a
    function. Any statement except an initialization or a declaration must
    go inside a function. Observe that you are not initializing the array
    elements, you are _assigning_ to them
    Also, if you want the array to be static then you should use the
    keyword static to indicate the same.
     
    Neelesh, Nov 6, 2005
    #10
  11. dof

    dof Guest

    Thanks Neelesh. I was trying to emulate some code I saw on the net that
    seems to be doing what I have listed below. I can't figure out how it worked
    because it surely doesn't work for me. To solve the problem, I changed the
    MESSAGES and CODES to classes; overrode the operator[]; and intialized the
    strings and codes in the constructor. It appears to give me the effect I was
    looking for.

    Take care,

    D


    "Neelesh" <> wrote in message
    news:...
    >
    > dof wrote:
    >
    >> Hi David,
    >>
    >> You're initializing the messages in main(). I'm trying to initialize them
    >> inline in the header. I want them static since I have several classes
    >> that
    >> include the header file containing those definitions. Maybe that's the
    >> problem?
    >>
    >> >> enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX};
    >> >>
    >> >> RWCString MESSAGES[ENUM_LIST_MAX]; <---------------------
    >> >>
    >> >> MESSAGES[A] = "MESSAGE_A";
    >> >> MESSAGES = "MESSAGE_B";
    >> >> ...
    >> >> MESSAGES[E] = "MESSAGE_E";

    >
    > dof wrote:
    >> Hi David,
    >>
    >> You're initializing the messages in main(). I'm trying to initialize them
    >> inline in the header. I want them static since I have several classes
    >> that
    >> include the header file containing those definitions. Maybe that's the
    >> problem?

    >
    > You cannot write general statements in a global-scope outside a
    > function. Any statement except an initialization or a declaration must
    > go inside a function. Observe that you are not initializing the array
    > elements, you are _assigning_ to them
    > Also, if you want the array to be static then you should use the
    > keyword static to indicate the same.
    >
     
    dof, Nov 6, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    549
  2. Shannon

    enum as array index

    Shannon, Jun 26, 2009, in forum: VHDL
    Replies:
    6
    Views:
    714
  3. Shawn W_
    Replies:
    5
    Views:
    289
    Aldric Giacomoni
    Sep 16, 2009
  4. ngoc
    Replies:
    5
    Views:
    180
    Tad McClellan
    May 11, 2006
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    307
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page