【发布时间】:2018-01-21 07:06:36
【问题描述】:
我在功能方面有一点问题。我相信这很可能是因为我没有正确使用它们。我的代码如下:
int duration(string fraction)
{
// X part of the fraction
int numerator = fraction[0];
// Y part of the fraction
int denominator = fraction[2];
// Checking for eighth note, quarter note and half note
if (numerator == 1)
{
switch (denominator)
{
case 8:
return 1;
case 4:
return 2;
case 2:
return 4;
}
}
// Checking for dotted quarter note
else
return 3;
}
我收到此特定错误的代码有什么问题:
错误:控制可能到达非空函数的结尾 [-Werror,-Wreturn-type]
【问题讨论】:
-
自己检查一下。如果
numerator是1,但denominator是,比如16。函数会返回什么? -
编译器不知道你只有八分音符、四分音符和二分音符。在你的 switch 中添加一个
defaultcase,表明不可能发生的事情。 -
else在这里是多余的。 -
switch()语句缺少default:的情况,因此会留下一个不包含return value语句的执行路径。
标签: c