-1
 switch (ch)
    {
    case '~' || 177: 
        tile->tileType = TILE_NONE;
        return true;
    case '@' || 219: //error here: '@'
        tile->tileType = TILE_WALL;
        return true; 
    }

The error reads: "case value '1' already used"

.....what the hell??

I've tried to clean the solution, restarted, used another PC. I'm "this close" to just reinstalling VS. Any idea what is happening?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Xolin_
  • 53
  • 1
  • 8

4 Answers4

8

You cannot have more than one case divided by || like you would an if statement. You have the case

'~' || 177:

Which is a boolean expression that evaluates to true, which gets evaluated to 1 because cases can only be an integral type (int, char and enum), so it must be converted to an int. So you are really saying

case 1:

The same thing happens in your second case statement, which is evaluated to a second case 1:, hence the error

Instead do:

case '~':
case 117:
   //code
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
2

Instead of using a logical OR expression use the switch fall-through behavior:

switch (ch)
{
   case '~':
   case 117:
      tile->tileType = TILE_NONE;
      return true;
   case '@':
   case 219:
      tile->tileType = TILE_WALL;
      return true;
}
Jim Rogers
  • 4,822
  • 1
  • 11
  • 24
1

'~' || 177 evaluates to true, which is 1.
So the line case '~' || 177: is really case 1:

Same with '@' || 219 in the line case '@' || 219:

You cannot have

switch (ch)
{
case 1: 
    tile->tileType = TILE_NONE;
    return true;
case 1: //  "case value '1' already used"  (You used it just above!)
    tile->tileType = TILE_WALL;
    return true; 
}

The error message is perfect and precise.
Why did you think you should reboot your PC or reinstall the IDE tools?

abelenky
  • 63,815
  • 23
  • 109
  • 159
0

the || operator just checks if any o the expressions at the sides is different from 0, and generates a true value of 1 if any of them is different from 0, so the first case is:

switch(...) {
case 1:

and the second case is also:

case 1:

so, the compiler cannot decide which case to follow.

Probably what you mean (and want to do) is this other thing:

switch (ch)
    {
    case '~': case 177: /* ch is either '~' or 177 */
        tile->tileType = TILE_NONE;
        return true;
    case '@': case 219: /* ch is either '@' or 219 */
        tile->tileType = TILE_WALL;
        return true; 
    }

which means that in case ch is any of '~' or 177, do the first part, and when it is '@' or 219, then go to the second part of the above code.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31