underflowError

Sorry for the simple question but for some reason I'm having a hard time finding a reference to show me exactly how to use this feature correctly. My book has it coded as such.

1
2
    if (isEmpty())
        throw UnderflowError();


However, the problem is that it shows in header file being included and gives unknown declaration for the UnderflowError() call.

On top of that my program will build correctly at this point but still has an unresolved identifier on the side so I'm not actually sure if its correctly defined.
Any help?
Last edited on
What are you having a problem with? UnderflowError is just a made up function. I believe the thought behind this in the book is that if an object is empty, it's considered an underflow, the opposite of an overflow. If the book came with some files, you may find the UnderflowError definition in one of the files that's included. If you have it, you may not have it linked properly. Can you elaborate a bit more?

It tends to help if you supply some errors along with the code the error is specifying. If the code is too large to post, you can use pastebin, or just provide a snippet.
Last edited on
Hmm, yeah you might be right they might just be making it up, that could easily make sense. I just know I've used "throw" in a project before and I assumed it was an operation for a class in the standard lib.

I had other errors that were stopping me from seeing an error message for the underflowError call. But here are the error message from the call:

minHeap.h:55:30: error: there are no arguments to ‘UnderflowError’ that depend on a template parameter, so a declaration of ‘UnderflowError’ must be available
minHeap.h:55:30: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
Can I accomplish the same thing essentially by using a break or return after the if condition? And skip the nonsense?
Which, isEmpty existing makes sense. I believe they're just trying to say, hey, throw the error that there isn't anything in the tree. If you're trying to, let's say, remove an item from the tree, you'd crash the program (without proper error handling) which is where this exception comes in handy. It's a bit odd how they decided to have you do it though.

minHeap.h:55:30: error: there are no arguments to ‘UnderflowError’ that depend on a template parameter, so a declaration of ‘UnderflowError’ must be available

That makes me think the function actually does exist, but as a template. Is there any way you can copy/paste your files to pastebin and give me the links to them?

Edit: Yes, you can. But it really depends on the situation. Sometimes you don't want to exit, sometimes you do. Sometimes you don't want a response, other times you do.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
template <typename Comparable>
class minHeap{

public:
    explicit minHeap(int capacity = 100);
    explicit minHeap(const vector<Comparable> & items);
    
    bool isEmpty() const;
    const Comparable & findMin() const;
    
    void insert(const Comparable & x);
    void deleteMin();
    void deleteMin(Comparable & minItem);
    void makeEmpty();
    
    private:
        int currentSize;
        vector<Comparable> array;
        
        void buildHeap();
        void percolateDown(int hole);
};



template <typename Comparable>
void minHeap<Comparable>::deleteMin(){
    
    if (isEmpty())
        return;
    
    array[1] = array[currentSize--];
    percolateDown(1);
}



Good guess, the statement is in a delete function. I've replaced the throw line with return statements in the delete function.
This seems to work though. I think they might be using a new/older declaration of the method. This seems to be the correct syntax. I'm wondering however what it's supposed to do; I don't much like using something I don't understand. Is throw supposed to alert the user to some kind of impossible calculation?

1
2
3
   
 if (isEmpty())
        throw underflow_error();



It's part of exception handling. You can throw an error for later viewing, like an error log, display it on exit, etc. Here is the cplusplus.com tutorial on exceptions: http://www.cplusplus.com/doc/tutorial/exceptions/

I haven't dealt with errors too much, but I believe you can specify exactly what you want to provide more detail. I'm actually going to read up on it some more myself, however, underflow_error is a standard error in the std library. You can find more on that here: http://www.cplusplus.com/reference/stdexcept/underflow_error/
Topic archived. No new replies allowed.