Enum list as array index problem

D

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 = "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;

}
 
D

David Hilsee

dof said:
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.
 
D

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 = "MESSAGE_B";
....
MESSAGES[E] = "MESSAGE_E";


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

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

Thanks,

D
David Hilsee said:
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.
 
D

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 = "MESSAGE_B";
....
MESSAGES[E] = "MESSAGE_E";


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

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

Thanks,

D
David Hilsee said:
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.
 
R

red floyd

dof said:
[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.
 
D

dof

LOL. Our coding standards require constants in caps.

D

red floyd said:
dof said:
[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.
 
D

David Hilsee

dof said:
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.
 
D

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 said:
dof said:
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.
 
D

David Hilsee

dof said:
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.
 
N

Neelesh

dof said:
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";

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.
 
D

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 said:
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";

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top