Hello, i started a thread last week about a problem i had and i couldnt solve it.
I got a tips to use constexpr but i felt it would make my code very unstructured after a while. I am starting a new thread because.
1. I wont get more responses on the last one now.
2. This is a bit different thread than the last one
So now i have made up an example to reproduce the problem i had in my last thread.
So my problem is. I have a global namespace which contains a TileType pointer.
when i am trying to use this in a case expression i get told by the compiler that my expression isnt constant. Why is that?
#include <iostream>
#include "global.h"
#include "test.h"
global::TileType * global::GrassTile = new global::TileType(1,5,4);
void main()
{
int i = 1;
switch(i)
{
case global::GrassTile->ID: //<-This is the line i am getting the error on
//do something
break;
}
char f;
std::cin >> f;
}
case statements can only handle constant values, either through literals or const declaration. If you need to use variables in the checks, use if statements instead.
Using if statements sounds like a possible solution, tho it sounds like an "ugly" solution. I tried const declaration and i still got the same error. Maybe i did something wrong. Can you please edit my code and show how you mean?
By the way, what is literals? Never heard that word before.
I have never used enums, i have just read about the slightly but this might be the time for me to start use them, i think they will do what is required, Thank you for your help i will mark this as solved now!
Yes, it's a cast operator. In this case it allows us to refer to a Tile object as if it were a TileID.
Your Tile class has several private variables (ID, Properties and TextureID). Since they are private we can't access them outside the class, so if we want to use them we have two choices:
1) write a "getter" function, as you did with getID()
2) Use a cast operator
Case labels must be known at compile-time; functions are executed during run-time. Just because a variable/object is declared constant, it doesn't mean it will be evaluated at compile-time.
Not sure what you tried to change to const, but as Framework pointed out, you can't use a function call as a case label.
Also, youa code isn't making a whole lot of sense.
Here you're assigning a constant (the enum value of ENVIRONMENT) to an int. Not a good idea, since you loose the type of what ENVIRONMENT is (TileProperties).
int i = global::ENVIROMENT;
Then you switch on the value of i (which was a constant)
1 2 3 4 5
switch(i)
{ case global::Grass->getID():
// some code
break;
}
What is it that "some code" is supposed to be doing? Is it related to TileProperties or to TileID?
What I think you want is something along these lines:
1 2 3 4 5 6 7 8 9 10
TileID id;
id = sometile->getID();
switch (id)
{
case GRASS: //Do something related to grass
break;
case ROAD_DIRT: // Do something related to road dir
break;
}
or, the following:
1 2 3 4 5 6 7 8 9 10
TileProperties tp;
tp = sometile->getProperties();
switch (tp)
{
case ENVIRONMENT: // Do something related to the environment
break;
case ROAD: // Do something related to the road
break;
}