lundi 27 juin 2016

if-else Block Raises a "control reaches end of non-void function" Warning

I'm creating a collision checking functions based on thee geometric shapes: lines, rectangles and circles. I wrote a Line-Line checking function which looks something like this:

bool check_collision(jLine L1, jLine L2, double singular_threshold=1e-6){
    /*Variable definitions omitted for brevity*/

    if (parallel){
        return false;
    }
    else if (infinite){
        return true;
    }
    else{
        jVector t;
        t = M.inverse()*(s2 - s1);
        double t1 = t.get_x(), t2 = t.get_y();

        on1 = (0. <= t1) and (t1 <= 1.);
        on2 = (0. <= t2) and (t2 <= 1.);

        if (seg1){
            if (not on1){
                return false;
            }
            else if (seg2){
                if (not on2){
                    return false;
                }
                else{
                    return true;
                }
            }
        }
        else{
            if (on2){
                return true;
            }
            else{
                return false;
            }
        }
    }
}

All variables and types are properly defined and do not cause any problems. However, when I compile this code, and warning is given that "control reaches end of non-void function". This warning seems to indicate that my if-else tree contains a branch where there is no return statement. However, as far as I can tell, every branch ends in the return of a bool type object. Am I missing something?

The warning is removed when I add a return true; at the very end of the function, but I feel as though this should not be necessary. I also hypothesized that this warning is raised anytime an if-else chain is used. I refuted this with another function that consisted entirely of an if-elif-else chain:

bool check_collision(jRect R1, jRect R2){
    if (R2.left() > R1.right()){
        return false;
    }
    else if (R2.right() < R1.left()){
        return false;
    }
    else if (R2.top() > R1.bottom()){
        return false;
    }
    else if (R2.bottom() < R1.top()){
        return false;
    }
    else{
        return true;
    }
}

The warning is not raised when this function is compiled. Can anybody shed some light on what I'm missing? Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire