By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

Enum list as array index problem

P: n/a
dof
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[b] = "MESSAGE_B";
....
MESSAGES{ENUM_LIST_MAX] = "";


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

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

}
Nov 5 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
>"dof" <do*****@comcast.net> wrote in message
news:Xq******************************@comcast.com. ..
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[b] = "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[b] = 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
Nov 5 '05 #2

P: n/a
dof
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[b] = "MESSAGE_B";
....
MESSAGES[E] = "MESSAGE_E";
unsigned int CODES[ENUM_LIST_MAX]; <-----------------------

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

Thanks,

D
"David Hilsee" <da*************@yahoo.com> wrote in message
news:88******************************@comcast.com. ..
"dof" <do*****@comcast.net> wrote in message

news:Xq******************************@comcast.com. ..
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[b] = "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[b] = 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

Nov 5 '05 #3

P: n/a
dof
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[b] = "MESSAGE_B";
....
MESSAGES[E] = "MESSAGE_E";
unsigned int CODES[ENUM_LIST_MAX]; <-----------------------

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

Thanks,

D
"David Hilsee" <da*************@yahoo.com> wrote in message
news:88******************************@comcast.com. ..
"dof" <do*****@comcast.net> wrote in message

news:Xq******************************@comcast.com. ..
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[b] = "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[b] = 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


Nov 5 '05 #4

P: n/a
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.
Nov 5 '05 #5

P: n/a
dof
LOL. Our coding standards require constants in caps.

D

"red floyd" <no*****@here.dude> wrote in message
news:FY*****************@newssvr13.news.prodigy.co m...
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.

Nov 5 '05 #6

P: n/a
"dof" <do*****@comcast.net> wrote in message
news:t6******************************@comcast.com. ..
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[b] = "MESSAGE_B";
...
MESSAGES[E] = "MESSAGE_E";
unsigned int CODES[ENUM_LIST_MAX]; <-----------------------

CODES[A] = 0x0010;
CODES[b] = 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[b] = "bar";

unsigned int CODES[ENUM_LIST_MAX];

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

I suspect that you are omitting something important.

--
David Hilsee
Nov 5 '05 #7

P: n/a
dof
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" <da*************@yahoo.com> wrote in message
news:rY******************************@comcast.com. ..
"dof" <do*****@comcast.net> wrote in message
news:t6******************************@comcast.com. ..
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[b] = "MESSAGE_B";
...
MESSAGES[E] = "MESSAGE_E";
unsigned int CODES[ENUM_LIST_MAX]; <-----------------------

CODES[A] = 0x0010;
CODES[b] = 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[b] = "bar";

unsigned int CODES[ENUM_LIST_MAX];

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

I suspect that you are omitting something important.

--
David Hilsee

Nov 5 '05 #8

P: n/a
"dof" <NO***********@comcast.netMAPSON> wrote in message
news:SY******************************@comcast.com. ..
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
Nov 5 '05 #9

P: n/a

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[b] = "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.

Nov 6 '05 #10

P: n/a
dof
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" <ne***********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

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[b] = "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.

Nov 6 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.