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?
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.
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)
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.
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?
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/