这不编译,我不知道this.please中的错误合作,因为我是一个初学者

时间:2015-03-29 07:26:02

标签: c++

template<class Type> Type ArrayPQ<Type>::removeMin(void ) throw(exception) 
{
    if (isEmpty())
    {
        cout << "Empty Priority Queue\n";
    }
    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.

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

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

然后将空检查更改为:

if (isEmpty())
{
    throw myex;
}