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

前端 未结 7 1280
梦谈多话
梦谈多话 2020-12-20 07:18

The exact warning I get is

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

and hand::show is

std::ostr         


        
7条回答
  •  遥遥无期
    2020-12-20 07:55

    If side is not left or right, then the return value is undefined.

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

    • 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).
    • side might be uninitialized, so it could be neither left nor right
    • side might have been assigned another value via typecasting, e.g. *((int*)&side) = 2

    Possible solutions include:

    • Replace the second if with an else as suggested by sth.
    • Change it to be:

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

提交回复
热议问题