user98188
user98188

Reputation:

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

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?

Upvotes: 3

Views: 57366

Answers (7)

Mr Fooz
Mr Fooz

Reputation: 111876

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

Upvotes: 6

SingleNegationElimination
SingleNegationElimination

Reputation: 156188

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.

Upvotes: 1

MatrixFrog
MatrixFrog

Reputation:

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

Upvotes: 3

sth
sth

Reputation: 229643

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];
    }
}

Upvotes: 2

Adrian Lopez
Adrian Lopez

Reputation: 1853

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.

Upvotes: 20

Eddie
Eddie

Reputation: 54421

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];
}

Upvotes: 4

Nathaniel Flath
Nathaniel Flath

Reputation: 16015

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.

Upvotes: 6

Related Questions