C++ 错误:控制可能到达非空函数的结尾/

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19104216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:30:38  来源:igfitidea点击:

ERROR: Control may reach end of non-void function /

c++functioncontrolswarningsvoid

提问by Tyler

Don't understand why i'm getting the warning that "Control may reach end of non-void function." Any suggestions?

不明白为什么我会收到“控制可能达到非空函数结束”的警告。有什么建议?

Here is my code:

这是我的代码:

long double geo_fn(long reps, int seed) {
    double x, y, pythag_sum, total = 0, inside = 0;
    default_random_engine engine(seed);             
    uniform_real_distribution<double>dist(0,1);

    for(int i = 0; i< reps ;i++){
        x = dist(engine);
        y = dist(engine);
        pythag_sum = (x*x)+(y*y);
        if (pythag_sum <= 1){
            inside += pythag_sum;
            total += pythag_sum;
        }
        else {
            total += pythag_sum;
         }
     return (inside/total)/10;
     }
}

回答by Dietmar Kühl

If you call the function with a first argument of 0the loop is never executed and the returnstatement is, thus, never reached. Falling of the end of a non-voidfunction is undefined behavior.

如果您使用0循环的第一个参数调用该函数,则永远不会执行该return语句,因此永远不会到达该语句。非void函数结束的下降是未定义的行为。

My personal guess is that the returnstatement was meant to one level up, i.e., in the out block: this would guarantee that the function always returns a value and the warning would go away.

我个人的猜测是该return语句的意思是上一层,即在 out 块中:这将保证函数总是返回一个值并且警告会消失。

回答by Molegrammer

you have to make sure,regardless of your expectations, that there will be always something to return, since you declared your function return type as non-void. for this code you will need a return statement that occur in the "else" case

你必须确保,不管你的期望如何,总会有一些东西要返回,因为你声明了你的函数返回类型为非空。对于此代码,您将需要在“else”情况下出现的 return 语句

Image* Image::findAndClone( imageType type )
{

    if (_prototypes[i]->returnType() == type)
        return _prototypes[i]->clone();

};

it will be:

这将是:

Image* Image::findAndClone( imageType type )
{

    if (_prototypes[i]->returnType() == type)
        return _prototypes[i]->clone();
    else return NULL;

};

in the case of a "for" loop, there is a chance that the loop will break at "i < _nextSlot" without actually returning anything. The compiler can't tell. so you will need to provide a return statement to capture case where the "for" loop "never executes".

在“for”循环的情况下,循环可能会在“i < _nextSlot”处中断,而实际上没有返回任何内容。编译器无法分辨。所以你需要提供一个 return 语句来捕获“for”循环“从不执行”的情况。

Image* Image::findAndClone( imageType type )
{
    for (int i=0; i < _nextSlot; i++)
            return _prototypes[i]->clone();
    return NULL;
};

then this is just a final example combining both:

那么这只是结合两者的最后一个例子:

Image* Image::findAndClone( imageType type )
{
    for (int i=0; i < _nextSlot; i++)
        if (_prototypes[i]->returnType() == type)
            return _prototypes[i]->clone();
    return NULL;
};

回答by GH14

Make sure to add an invalid parameter check before the for loop. Making sure that the function will return an error code if one of the parameters passed has an invalid value.

确保在 for 循环之前添加无效参数检查。如果传递的参数之一具有无效值,请确保该函数将返回错误代码。