得到警告:当试图编译时,控件可能会到达非void函数的末尾

Getting warning: control may reach end of non-void function when trying to compile

本文关键字:void 函数 控件 警告 编译      更新时间:2023-10-16

这不会编译,我不知道其中的错误:

template<class Type> Type ArrayPQ<Type>::removeMin(void ) throw(exception) 
{
    if (isEmpty())
    {
        cout << "Empty Priority Queuen";
    }
    else
    {
        Type min = array[0];
        array[0] = array[heap];
        heap--;
        minHeapify(0);
        return min;
    }
}

我一直收到这样的警告:

In file included from runtime_analysis.cpp:7:
./PQ3.h:57:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
./PQ3.h:114:9: note: in instantiation of member function 'ArrayPQ<int>::removeMin' requested here
removeMin();
^
runtime_analysis.cpp:67:12: note: in instantiation of member function 'ArrayPQ<int>::DeleteAll' requested here
ArrPQ->DeleteAll();
^
1 warning generated.

每当我运行代码时,我都会收到一个错误。

您收到警告是因为您保留了可能无法到达返回语句的开放场景。目前您只有一个,在else语句范围内。在else语句的范围之外,您将返回什么?

如果队列为空,则应抛出异常:

class myexception: public exception
{
    virtual const char* what() const throw()
    {
        return "Empty Priority Queue";
    }
} myex;

然后将空复选框改为:

if (isEmpty())
{
    throw myex;
}