473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Enum list as array index problem

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
10 9049
>"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
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
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
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
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
"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
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
"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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PhilC | last post by:
Hi Folks, If I have an array holding a pair of numbers, and that pairing is unique, is there a way that I can find the array index number for that pair? Thanks, PhilC
16
by: ES Kim | last post by:
"A Book on C" explains a technique to use an arbitrary array index range. int* p = malloc(sizeof(int) * 10) - 1; This way, the allocated array can be accessed with index range 1 ~ 10, not 0 ~...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
2
by: Gregg Teehan | last post by:
Suggestions on best practive for array declare / subscript using an enum - this is easy in Delphi / Object Pascal. I want private MyObjectClass myObjects; myObjects = new MyObjectClass ;...
3
by: Richard | last post by:
Okay gang, This should be simple but apparently it's not... I want to use the System.DayOfWeek enum to create and access an array of objects with one object for each day of the week. I'd like...
8
by: Joe Rattz | last post by:
Ok, I can't believe what I am seeing. I am sure I do this other places with no problems, but I sure can't here. I have some code that is indexing into the ItemArray in a DataSet's DataRow. I...
13
by: Eugene Rice | last post by:
I'm writing C code for an Atmel AVR micro controller. Because RAM space is extremely limited (about 500 bytes) I use char variables wherever I can. For example I use chars as array indices in...
26
by: Szabolcs Nagy | last post by:
what type can/should be used as array index? eg. can i use unsigned long? or should it be size_t? example: int sum(int *a, index_t n) { int s = 0; index_t i;
4
by: BravoFoxtrot | last post by:
Hi, I'm trying to build an index into a multi dimensional associative array. I may not know how many dimensions there are so i want to pass the array indexes as a variable. $arrayToAccess =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.