I'm making a heap class and I want one of my functions, const Employee& Heap::top() const, to return a reference to the top element or throw an error if the heap is empty.
I've never used try, catch, or throw statements before so I'm having trouble getting it to work the way I want.
Here is the function def:
const Employee& Heap::top() const // Not Working
{
try{
if(mySize > 0)
return a[0];
else
throw "Heap is empty!";
}catch(char *str){
cout << "Error: " << str << endl;
}
}
The function will get to the catch statement if my heap is empty but then it will still return a random value, which I'm guessing is a[0]. Is there a way I can return an error rather than an Employee reference?
I'm hoping to find the correct way of doing this.