控制到达非空函数结束[-Werror=return-type]
control reaches end of non-void function [-Werror=return-type]
即使使用默认的else语句,我也会得到这个错误
int max_of_four(int a, int b, int c, int d)
{
if((a>b)&&(a>c))
{
if(a>d)
return a;
}
else if((b>c)&&(b>d))
{
return b;
}
else if(c>d)
{
return c;
}
else
return d;
}
你的第一个if
是问题
if((a>b)&&(a>c))
{
if(a>d)
return a;
// what about else?
}
如果您的外部条件是true
,而内部条件是false
,则不会有任何return
情况。
#include <algorithm>
int max_of_four(int a, int b, int c, int d)
{
return std::max(std::max(a, b), std::max(c, d));
}
也可以用
#include <algorithm>
int max_of_four(int a, int b, int c, int d)
{
return std::max({a, b, c, d});
}
> " control到达non-void function end [-Werror=return-type] "错误发生在编译阶段,当你的非void函数(定义为返回值)有可能无法返回值时(分支)。虽然,这取决于输入,但它不一定到达那个死胡同。
如果:
int max_of_four(int a, int b, int c, int d)
{
if((a>b)&&(a>c))
{
if(a>d)
return a;
else
return d;
}
else if((b>c)&&(b>d))
{
return b;
}
else if(c>d)
{
return c;
}
else
return d;
}
相关文章:
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- 为什么我收到以下错误:"returning reference to temporary [-Werror=return-local-addr]"
- 按参数类型更改"return type template"
- std::tuple::get return const Type&&
- 在 Ubuntu 14.04 上构建 AWS c++ 配置库时出现"return type is incomplete"错误
- 如何修复空虚拟方法的"warning: no return statement in function returning non-void [-Wreturn-type]"
- 犰狳/ Xcode:无处"functions that differ only in their return type cannot be overloaded"错误
- 控制达到非空隙函数的末端[-werror = return-type](只是一个简单的法官)
- IDL 接口,C++,CORBA,我正在处理"conflicting return type specified for"和"invalid abstract return type for memb
- "Cannot overload functions distinguished by return type alone"是什么意思?
- "Cannot overload functions by return type alone"错误
- 避免重复变量类型:Return type Resolver、C++11 auto、..其他任何东西
- "overloaded function differs only by return type"错误
- 指定类对象的"return type"
- 警告:控制到达非空函数结束[- return-type]
- 在这段代码中,我有一个警告:控制达到非void函数的结束[- return-type].我怎样才能解决这个问题
- 控制到达非空函数结束[-Werror=return-type]