【发布时间】:2021-12-15 11:55:36
【问题描述】:
我有一个基于 int 比较的 switch 语句,但由于某种原因,它在真正的比较上一直失败,并跳到默认值。这是我的代码:
string Error(int errorNum, int send=1, int crash=1)
{
string errorType;
switch(errorNum)
{
case 10:
errorType = "Port number";
...
default:
errorType = "Unknown";
}
...
}
我一直用10 的参数调用它,但是当我这样做时,它失败了。等价的if... else if... else 方法有效,这里也是:
string Error(int errorNum, int send=1, int crash=1)
{
string errorType;
if (errorNum == 10) // Normally I'd use braces,
errorType = "Port number"; // but here, it just clutters
...
else
errorType = "Unknown";
...
}
【问题讨论】:
-
您没有错过添加您的
breaks,是吗? -
在
default之前有break吗?如果不是,那么它看起来就像是在命中default,因为它会掉下来。
标签: c++ int switch-statement