“警告:并非所有控制路径都返回值”是什么意思?(C++)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/928694/
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 18:00:58  来源:igfitidea点击:

What does "warning: not all control paths return a value" mean? (C++)

c++controlswarnings

提问by SingleNegationElimination

The exact warning I get is

我得到的确切警告是

warning C4715: 'hand::show' : not all control paths return a value

and hand::show is

和手::显示是

std::ostream& hand::show(std::ostream& os) const
{
    if(side == left)
    {
        return os<<display[0]<<display[1]<<display[2]<<display[3]<<display[4];
    }
    if(side == right)
    {
        return os<<display[4]<<display[3]<<display[2]<<display[1]<<display[0];
    }
}

where side is a variable of type orientation

其中 side 是一个orientation类型的变量

orientation{
    left = -1,
    right = 1
};

What does the warning mean, and what would be the best solution to get rid of it?

警告是什么意思,摆脱它的最佳解决方案是什么?

回答by Adrian Lopez

Your compiler isn't smart enough to take into account that the only two options for sideare leftand right, so it thinks it's possible for neither return statement to be executed. When sideis neither leftnor right, your function doesn't say which value to return.

你的编译器是不够聪明考虑到的是,只有两个选项sideleftright,因此它认为它有可能要执行既不return语句。当side既不是也不left是时right,您的函数不会说明要返回哪个值。

回答by Nathaniel Flath

The error means that if side is neither left nor right, your function will not return a value - either side is declared improperly or your enum is. An enum should be defined like

该错误意味着如果 side 既不是 left 也不是 right,您的函数将不会返回值 - 任一侧声明不正确或您的 enum 是。枚举应定义为

enum orientation {left, right};

So try changing your orientation structure to that.

所以尝试改变你的方向结构。

回答by Mr Fooz

If sideis not leftor right, then the return value is undefined.

如果side不是leftright,则返回值未定义。

Even though orientationis an enum with only two values (right now), it can still have a different value for any of the following reasons:

即使orientation是只有两个值的枚举(现在),由于以下任何原因,它仍然可以具有不同的值:

  • In the future, you may change the header to include other values in the enum, so it's defensive programming to assume that this will happen (and your compiler is being nice and warning you now).
  • sidemight be uninitialized, so it could be neither leftnor right
  • sidemight have been assigned another value via typecasting, e.g. *((int*)&side) = 2
  • 将来,您可能会更改标头以在枚举中包含其他值,因此假设这会发生是防御性编程(并且您的编译器现在很好并警告您)。
  • side可能未初始化,所以它既不是left也不是right
  • side可能已经通过类型转换分配了另一个值,例如 *((int*)&side) = 2

Possible solutions include:

可能的解决方案包括:

  • Replace the second ifwith an elseas suggested by sth.
  • Change it to be:

    if(side == left) {
        return ...;
    } else if(side == right) {
        return ...;
    } else {
        ...handle error...
    }
    
  • 按照 sth 的建议将第二个替换为ifan else
  • 将其更改为:

    if(side == left) {
        return ...;
    } else if(side == right) {
        return ...;
    } else {
        ...handle error...
    }
    

回答by Eddie

The warning means it's possible to go through your method without returning any explicit value. With your code:

警告意味着可以在不返回任何显式值的情况下执行您的方法。使用您的代码:

std::ostream& hand::show(std::ostream& os) const
{
    if(side == left)
    {
        return os<<display[0]<<display[1]<<display[2]<<display[3]<<display[4];
    }
    if(side == right)
    {
        return os<<display[4]<<display[3]<<display[2]<<display[1]<<display[0];
    }
}

if side != leftand side != right, then you don't return anything. A common way of fixing this problem is to assume, for example, if not "left" then always assume "right":

如果side != leftside != right,那么你不返回任何东西。解决此问题的常见方法是假设,例如,如果不是“左”,则始终假设“右”:

std::ostream& hand::show(std::ostream& os) const
{
    if(side == left)
    {
        return os<<display[0]<<display[1]<<display[2]<<display[3]<<display[4];
    }
    return os<<display[4]<<display[3]<<display[2]<<display[1]<<display[0];
}

回答by Eddie

You can do what sth said, or, since in this case you're really returning the same thing either way...

你可以做某事,或者,因为在这种情况下,你真的会以任何一种方式返回同样的东西......

std::ostream& hand::show(std::ostream& os) const
{
    if(side == left)
    {
        os<<display[0]<<display[1]<<display[2]<<display[3]<<display[4];
    }
    else
    {
        os<<display[4]<<display[3]<<display[2]<<display[1]<<display[0];
    }
    return os;
}

回答by sth

To get rid of the warning, replace the second ifwith an else:

为了摆脱的警告,更换第二ifelse

std::ostream& hand::show(std::ostream& os) const
{
    if(side == left)
    {
        return os<<display[0]<<display[1]<<display[2]<<display[3]<<display[4];
    }
    else
    {
        return os<<display[4]<<display[3]<<display[2]<<display[1]<<display[0];
    }
}

回答by SingleNegationElimination

As others suggest, the problem is that your sidemight be neither leftnor right.
You may modify your function to do any of the following:

正如其他人所建议的那样,问题在于您side可能既不是left也不是right
您可以修改您的函数以执行以下任何操作:

  1. change the second if statement to an else, or remove the condition all together, since if the side is not left, it must be right.
  2. follow Nathaniel Flath's suggestion and modify the orientation type to an enum.
  3. raise an exception as the last statement of the function.
  1. 将第二个 if 语句更改为 else,或者将条件一起删除,因为如果一侧不是左边,它一定是右边。
  2. 按照 Nathaniel Flath 的建议,将方向类型修改为枚举。
  3. 引发异常作为函数的最后一条语句。