简体   繁体   中英

C++ warning: Not all control paths return a value

Well, I have some warnings that cause my program to crash, when I enter size 3.
Not all control paths return a value.

I am trying to solve N matrix, input, output and some operations. I store first column

 _vec[0:size-1],last column _vec[size : (size*2)-1]

and diagonal

 _vec[size*2 : size*3-2]

of matrix in 1-dimensional array. The size of array is size of matrix * 3 -2 . The problem occurs when I overload () operators:

int _size = (_vec.size() +2) /3;   
// when I switch from vector size to normal matrix size. f.e vector size: 7,
// my matrix size is 3. 

int Matrix::operator()(int i, int j) const
    {
        int _size = (_vec.size() +2) /3;
        if ((i >= _size || i < 0) || (j >= _size || j < 0)) throw OVERINDEXED;
        if (i != j && j != 0 && j != _size - 1) return 0;
        else {
            if (j == 0)
            {
                return _vec[i];
            }
            else if (j == _size - 1)
            {
                return _vec[_size + i];
            }
            else if (i == j && j != 0 && j != _size - 1)
            {
                return _vec[(_size * 2) + i];
            }
        }
    }

    int& Matrix::operator()(int i, int j)
    {
        int _size = (_vec.size() +2) /3;
        if ((i >= _size || i < 0) || (j >= _size || j < 0)) throw OVERINDEXED;
        if (i != j && j != 0 && j != _size - 1) throw NULLPART;
        else {
            if (j == 0)
            {
                return _vec[i];
            }
            else if (j == _size - 1)
            {
                return _vec[_size + i];
            }
            else if (i == j && j != 0 && j != _size - 1)
            {
                return _vec[(_size * 2) + i];
            }
        }
    }

Change the code like below for second function as well.

int Matrix::operator()(int i, int j) const
{
    int _size = (_vec.size() + 2) / 3;
    if ((i >= _size || i < 0) || (j >= _size || j < 0)) throw OVERINDEXED;
    if (i != j && j != 0 && j != _size - 1) return 0;
    else {
        if (j == 0)
        {
            return _vec[i];
        }
        else if (j == _size - 1)
        {
            return _vec[_size + i];
        }
        else if (i == j && j != 0 && j != _size - 1)
        {
            return _vec[(_size * 2) + i];
        }
    }
    return 0; // added this line
}

It requires some analysis to prove that all cases return.

And it seems your compiler doesn't do the full analysis

int Matrix::operator()(int i, int j) const
{
    int _size = (_vec.size() + 2) /3;
    if ((i >= _size || i < 0) || (j >= _size || j < 0)) throw OVERINDEXED;
    if (i != j && j != 0 && j != _size - 1) return 0;
    else {
        if (j == 0)
        {
            return _vec[i];
        }
        else if (j == _size - 1)
        {
            return _vec[_size + i];
        }
        else if (i == j && j != 0 && j != _size - 1)
        {
            return _vec[(_size * 2) + i];
        }
        else
        {
             // No return here.
             // But is this case reachable?
             // yes, for (i, j) respecting:
             //    (0 <= i && i < _size) && (0 <= j && j < _size)
             //    && ((i == j) || (j == 0) || (j == _size - 1)) // #2
             //    && (j != 0) && (j != _size - 1)               // #1
             //    && (i != j || j == 0 || j == _size - 1)       // #3
             // which after simplification results indeed in false.
             // #1 simplifies #2 to (i == j) and #3 to (i != j)
        }
    }
}

On the other part, that means that you do useless "tests" that you can remove (and so please the compiler):

int Matrix::operator()(int i, int j) const
{
    int _size = (_vec.size() + 2) /3;
    if ((i >= _size || i < 0) || (j >= _size || j < 0)) throw OVERINDEXED;
    if (i != j && j != 0 && j != _size - 1) return 0;
    else {
        if (j == 0)
        {
            return _vec[i];
        }
        else if (j == _size - 1)
        {
            return _vec[_size + i];
        }
        else // We have necessary (i == j && j != 0 && j != _size - 1)
        {
            return _vec[(_size * 2) + i];
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM