bo循环中的bool函数出错 [英] Error in bool function with for loop

查看:94
本文介绍了bo循环中的bool函数出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么我会收到此错误。我检查括号和括号。循环不应该永远持续。



I'm not sure as to why I'm getting this error. I checked the brackets and parentheses. The loop shouldn't continue forever.

Error: helpers.c:43:1: error: control may reach end of non-void function [-Werror,-Wreturn-type] } ^



PS:在我的情况下我需要使用Werror。



谢谢



我的尝试:




PS: I'm required to use Werror in my case.

Thanks

What I have tried:

bool search(int value, int values[], int n) 
{
 if ( n < 0)
 { return false; }

for (int a = 0; a < n; a++)
{
    if ( values[a] == value)
    {
        return true;
        break;
    }

    else
    {
        if ( a == n)
        {
            return false;
        }
    }
}
}

推荐答案

您的 search 函数最后需要 return 语句。此外,在返回之后,您不需要 break 语句。事实上,您根本不需要第一个 if else 子句,您只需要:

Your search function requires a return statement at the end. Also, you do not require a break statement after a return. In fact you do not need the first if or the else clause at all, all you need is:
bool search(int value, int values[], int n) 
{
    for (int a = 0; a < n; a++)
    {
        if ( values[a] == value)
        {
            return true;
        }
    }
    return false;
}


循环内的代码充其量只是混乱而不是需要 if 语句的 else 子句。您的函数不会在所有代码路径上返回值。如果循环完成而没有满足您在 if 语句中设置的任何条件,会发生什么?编译器无法弄清楚,并且不能假设 for 循环永远不会完成。



你应该重写这个用于设置返回值的代码,而不是直接从循环内部返回。例如:

Your code inside the for loop is chaotic at best and doesn't need the else clause of the if statement. Your function doesn't return a value on all code paths. What happens if the loop completes without satisfying any of the conditions you set in those if statements? The compiler cannot figure that out and cannot assume the for loop will never complete.

You should be rewriting this code to set a return value instead of just straight out returning from inside the loop. For example:
bool search(int value, int values[], int n) 
{
    // The default is to return false.
    bool returnValue = false;

    // Is the n parameter OK?
    if ( n >= 0)
    {
        // Yes, so execute the search.
        for (int a = 0; a < n; a++)
        {
            if (values[a] == value)
            {
                returnValue = true;
                break;
            }
        }
    }

    return returnValue;
}


Quote:

错误:helpers.c :43:1:错误:控件可能会达到非空函数的结束[-Werror,-Wreturn-type]} ^

Error: helpers.c:43:1: error: control may reach end of non-void function [-Werror,-Wreturn-type] } ^



您收到此错误消息,因为它可能达到函数的结尾没有遇到返回


You get this error message because it is possible to reach the end of the function without encountering a return

for (int a = 0; a < n; a++)
{
    if ( values[a] == value)
    {
        return true;
        break;
    }

    else
    {
        if ( a == n)
        {
            // This line will never execute because a is never equal to n inside the loop
            return false;
        }
    }
}


这篇关于bo循环中的bool函数出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆