error c2450:switch expression of type 'float' is illegal
时间: 2024-05-09 08:19:38 浏览: 111
This error message is generated by the C++ compiler when a switch statement is used with a float type variable as its expression. In C++, switch statements can only be used with integral types such as int, char, and long.
To resolve this error, you can either convert the float variable to an integer using a type casting operator or change your code to use an if-else statement instead of a switch statement.
For example, instead of using:
```
float x = 2.5;
switch(x) {
case 1.0:
// do something
break;
case 2.0:
// do something else
break;
default:
// handle other cases
break;
}
```
You can use:
```
float x = 2.5;
int y = static_cast<int>(x); // convert float to int
if(y == 1) {
// do something
}
else if(y == 2) {
// do something else
}
else {
// handle other cases
}
```
阅读全文