Exceptions. (This should be easy)

This is a discussion on Exceptions. (This should be easy) within the C++ Programming forums, part of the General Programming Boards category; I want to have an exception for my stack class that is thrown if new memory cannot be allocated for ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    37

    Exceptions. (This should be easy)

    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:

    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
    };
    Implementation file:
    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;
    	}
    }
    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.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Why do you have a random NULL; just sitting there in your Push implementation?

    What are the errors? Where are they?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    The null was just a space holder from earlier, don't worry about it.

    As far as the errors, here's an ex.:
    c:\Program Files\Microsoft Visual Studio .NET\Vc7\include\streamb.h(90): error C2872: 'ios' : ambiguous symbol

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Code:
    if (newPointer == NULL)
            NULL;
            throw StackException("StackException: stack push cannot allocate memory");
    else
    {
    ...
    }
    Should be...
    Code:
    if (newPointer == NULL)
       throw StackException("StackException: stack push cannot allocate memory");
    else
    {
    ...
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    That didn't change anything. I had the null commented out when I compiled.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Then you'll need to give us all your code if you want to know where the erros are. VC++ compiled what you gave us just fine once "NULL;" was removed.

    gg

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    When I add an exception in the header/implementation file, what do I have to change in the application file?

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Declaring what a method may throw doesn't require any changes elsewhere inorder to make the code compile (Unless your writting Java).

    gg

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    I didn't think so. The header/impl files compile fine with the exception included but the app file goes to crap. When I take the exception class out, the app compiles fine. What's the deal?

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    What compiler/OS are you using? Post a zip of your project and I'll take a look at it.

    gg

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    Forget it, I just found it. I was using namespace std in .h file, not in .cpp file. Thanks for trying to help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg fault in easy, easy code
    By lisa1901 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2007, 04:28 AM
  2. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  3. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  4. causing default exceptions to throw additional info
    By lightatdawn in forum C++ Programming
    Replies: 14
    Last Post: 08-06-2003, 06:39 PM
  5. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21