views:

853

answers:

7

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

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

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

+7  A: 

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

enum orientation {left, right};

So try changing your orientation structure to that.

Nathaniel Flath
+2  A: 

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 != left and 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":

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];
}
Eddie
+7  A: 

Your compiler isn't smart enough to take into account that the only two options for side are left and right, so it thinks it's possible for neither return statement to be executed. When side is neither left nor right, your function doesn't say which value to return.

Adrian Lopez
+2  A: 

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

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];
    }
}
sth
+3  A: 

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;
}
+1  A: 

As others suggest, the problem is that your side might be neither left nor right.
You may modify your function to do any of the following:

  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.
TokenMacGuy
+2  A: 

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... }

Mr Fooz