I want to have an exception for my stack class that is thrown if new memory cannot be allocated for a push. From my book, I entered the following into my program:
Implementation file:Code:#include <exception> #include <string> typedef int StackType; using namespace std; class StackException : public exception { public: StackException(const string & Message = "") : exception(Message.c_str()) {} }; class Stack { public: Stack(); ~Stack(); bool isEmpty(); void push(StackType[2]) throw(StackException); void pop(StackType[2]); private: struct StackNode { StackType info[2]; //data item on the stack StackNode *next; //a pointer to the next node }; StackNode *topOfStack; //a pointer to the first node in stack };
When I went to compile my app it ran out with over a hundred errors! What's the deal? I'm sure I'm missing something easy.Code:void Stack::push(StackType newitem1[2]) { StackNode *newPointer = new StackNode; if (newPointer == NULL) NULL; throw StackException("StackException: stack push cannot allocate memory"); else { newPointer->info[0] = newitem1[0]; newPointer->info[1] = newitem1[1]; newPointer->next = topOfStack; topOfStack = newPointer; } }