add to enum items

enum Ship
{
SHIP_BATTLESHIP,
SHIP_AIRCRAFT_CARRIER,
SHIP_CARGO_SHIP,
SHIP_DESTROYER,
SHIP_BARGE,
SHIP_TUGBOAT,
};

How do I add SHIP_TITANIC to the list? I have tried static cast and it doesnt work/am not doing it right:

1
2
3
4
5
6
7
8
9
10
enum Ship
{
  SHIP_BATTLESHIP,
  SHIP_AIRCRAFT_CARRIER,
  SHIP_CARGO_SHIP,
  SHIP_DESTROYER,
  SHIP_BARGE,
  SHIP_TUGBOAT,
  SHIP_TITANIC
};


You can't change enums at runtime. Are you looking for a container of sorts?
Not quite. Let me restate the question.

Here is an enum list:

enum Color
{
COLOR_BLACK, // assigned 0
COLOR_RED, // assigned 1
COLOR_BLUE, // assigned 2
COLOR_GREEN, // assigned 3
COLOR_WHITE, // assigned 4
COLOR_CYAN, // assigned 5
COLOR_YELLOW, // assigned 6
COLOR_MAGENTA // assigned 7
};

Color color = static_cast<Color>(5); // ugly


What does this statement do? 5 of what? What equals 5?
What does this statement do? 5 of what? What equals 5?


Color color = COLOR_CYAN;
Last edited on
Color color = COLOR_CYAN;

but we already knew that from our list right?
Um... no. Your so-called "list" doesn't say anything about a variable called color.

Your enum statement is defining a type called Color.

This:

Color color = static_cast<Color>(5); // ugly

declares a variable called color of that type, and initialises its value to COLOR_CYAN.

This:

Color color = COLOR_CYAN;

does exactly the same thing.

EDIT: A little part of me dies inside every time I have to use the American spelling of "colour" :P
Last edited on
Oh. Got it. : ) Thx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum SHIPS
{
	SHIP_BATTLESHIP = -5,
	SHIP_AIRCRAFT_CARRIER,
	SHIP_CARGO_SHIP = 100,
	SHIP_DESTROYER,
	SHIP_BARGE,
	SHIP_TUGBOAT
};
...
int inputShip;
	cin >> inputShip;
	ship = static_cast<SHIPS>(inputShip);
	cout<<ship;
	SHIP_BATTLESHIP = ship;
	return 0;


error code:\src\main.cpp:48:18: error: lvalue required as left operand of assignment

I thought I handled it correctly (assigning a user nput to be assigned to an enumerated type)
SHIP_BATTLESHIP = ship; What are you trying to do here? This statment is equivalent to -5 = ship;
SHIP_BATTLESHIP = ship;

changed to this:

SHIPS SHIP_BATTLESHIP = ship

I am trying to allow the user to input and change the enum value.
Last edited on
You can't do that. That's like asking the user to enter a new value for the number "-5".

In your enum definition, you're telling the compiler that the symbol SHIP_BATTLESHIP means "-5". You can't change that at runtime.

Why would you even want the user to be able to change that value? What purpose would it serve?
Last edited on
You can't change that at runtime.


Thank you now it makes sense to me. I was trying to write a run time routine; I didn't know that you couldn't.

Why would you even want the user to be able to change that value? What purpose would it serve?


It wouldn't -- I have a better handle on this subject matter now even though I was trying to achieve the impossible. : )
Topic archived. No new replies allowed.