The compiler (Visual Studio 2017) does not recognize me
And he does well not to recognize it, because he is following the instructions of C ++ standard in section §8.16 (my translation):
8.16 Conditional Operator
[...]
If the first or second operand has type void
, one of the following conditions must be met:
The second or third operand (but not both) is a expression-throw (possibly in parentheses); the result is of the type and category of the other. The expression-conditional is a bit field if that operand is a bit field.
Both the second and the third operand are of type void
, the result is type void
and is a pure value of the right side [...].
Since none of the conditions to accept that any of the operands is of type void
, the program fails. But it's easy to solve:
(comprar >= 1 && comprar <= 40) ?
ActualizarAsientos(Asientos, comprar) :
(void)(cout << "Asiento fuera de rango");
// ~~~~~~ <-- conversión a void
If both expressions are of type void
, then §8.16.2.2 is met and compiled correctly. But it's not a good idea, follow the advice of abulafia and change the code by if
- else
:
if (comprar >= 1 && comprar <= 40)
ActualizarAsientos(Asientos, comprar);
else
cout << "Asiento fuera de rango";
Or make it comply with §8.16.2.1 and capture the exception somewhere:
(comprar >= 1 && comprar <= 40) ?
ActualizarAsientos(Asientos, comprar) :
(throw std::logic_error{"Asiento fuera de rango"});