473,616 Members | 2,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_L IST_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 9078
>"dof" <do*****@comcas t.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_L IST_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*****@comcas t.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_L IST_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*****@comcas t.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_L IST_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.d ude> wrote in message
news:FY******** *********@newss vr13.news.prodi gy.com...
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*****@comcas t.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*****@comcas t.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.netMAPS ON> 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","B AZ","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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11725
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
3685
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 ~ 9. You cannot use p, of course, and the memory should be released with free(p + 1). It worked with gcc and MSVC, but I'm not sure if it makes sense to take the address before the initial element of an array. Does anyone have any definite answer?
29
5443
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 elements as *(mptr+k) where I've declared MYTYPE *mptr; what should be the type of 'k'? Should it be ptrdiff_t?
2
11589
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 ; myObjects = new MyObjectClass(); myObjects = new MyObjectClass();
3
16059
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 the array declaration to be strongly typed to the enum and I'd like to use the enum to access entries in the array. I'd like to write code something like this: #using System;
8
2270
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 basically want to change the value of the data stored at a particular index in the ItemArray. Ideally, I would like for my code to look like this (pay attention to the index of ItemArray): row.ItemArray = itemArray.ToString().Substring(3);
13
7702
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 dozens of places: char arr; char x,y,z; for (x=0; x<3; x++) { arr = ...; // gcc gives warning here // more code }
26
8359
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
2275
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 = array( array('Data'=>array('id'=>1, 'title'=>'Manager')), array('Data'=>array('id'=>1, 'title'='Clerk')) );
0
8203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8146
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8297
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5550
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4063
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4141
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2579
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.