Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

c++ - class method not returning value

I have a method inside a class that is not returning a value. These are the relevant variables that are being used in the method, they are in the private part of the class

int hieght_of_plane = 0 ;
int position_of_plane = 0 ;
bool landing_gear = true ;
bool is_flying = false ;
bool is_alive = true ;

And here is the method that is not returning true or false, it is in the public section of the class.

    bool check_for_alive()
    {
        if (is_flying == false)
        {
            return true ;
        }
        if (is_flying == true)
        {
            if (hieght_of_plane <= 3)
            {
                if (landing_gear == false)
                {
                    is_alive = false ;
                    return false ;
                }
                else if (landing_gear == true)
                {
                    return true ;
                }
            }
        }
    }

The method seems to not do anything and then I get the no return value error

warning: control reaches end of non-void function [-Wreturn-type]

I assume that it should return false in this instance because is_flying = false so the method should return false, but it isn't.

question from:https://stackoverflow.com/questions/65911416/class-method-not-returning-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The case that falls through is when flying == true and hieght_of_plane is greater than 3.

    if (is_flying == true)
    {
        if (hieght_of_plane <= 3)
        {

The 2nd if statement does not have an else, which is necessary for returning a value.

Edit 1: An else clause
The else clause should be used for the "not" case of the if statement.
For example:

if (is_flying == false)
{
    return true;
} 
else // This means is_flying == true
{
    if (hieght_of_plane <= 3)
    {
        if (landing_gear == false)
        {
            is_alive = false ;
            return false ;
        }
        else // implies landing_gear == true
        {
            return true ;
        }
    else // implies hieght > 3
    {
         return ????
    }
}

You could also reduce this down to one Boolean expression.

Edit 2: Simplification
There are two functionalities here: Return true/false for a combination of conditions and to set is_alive based on a condition.
Let's say that one condition is true, all others is false.

bool check_for_alive()
{
    if (is_flying && (height_of_plane <= 3) && (landing_gear == false))
    {
        is_alive = false;
    }
    return is_flying && (height_of_plane <= 3) && (landing_gear == true);
}

Note: The above does not consider the case of `(height_of_plane > 3), since this is not specified in the OP's original code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...