Unhandled exception

This is a discussion on Unhandled exception within the C++ Programming forums, part of the General Programming Boards category; Hi, I have already spent some time trying to make work the following code . there is no error message ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    18

    Unhandled exception

    Hi,
    I have already spent some time trying to make work the following code . there is no error message , but an exception instead. the problem seems to come from the fonctions of the class Node and perhaps of the pointer Next which is not well defined.
    Code:
    #include<iostream>
    using namespace std;
    
    class UseNodes
    {
    private:
    	class Node
    	{
    	public:
    		Node()
    		{ 
    		    Data = 1;
    			Next = 0;
    		}
    		Node(int a):Data(a) {}
    		~Node(){};
    		Node* GetNext()const{ return Next;}
    		void SetNext( Node* A){ Next = A;}
    		int GetData()const{ return Data;}
    		void setData(int a) { Data = a;}
    	private:
    		Node* Next;
    		int Data;
    	};
    	Node* Head;
    	Node* Tail;
    
    public:
    	UseNodes(){Head = 0; Tail = 0; }
    	~UseNodes(){};
    	void AddNode(int a)
    	{
    		Node* Temp = new Node(a);
    		if(Head==0)
    		{
    			Head = Temp;
    			Tail = Temp;
    		}
    		else
    		{
    			Node* Current = new Node();
    			Current = Head;
    		    while(Current)
    			{
    				Current = Current->GetNext();
    			}
    			Current->SetNext(Temp);
    			Tail = Temp;
    		}
    	}
    	void Print()const
    	{
    		int a = 2;
    		Node* Temp = new Node(a);
    		if(Head==0)
    		   cout << " Empty list.\n " ;
    		else
    		{
    			Temp = Head;
    			cout << Head->GetData() << endl;
    			while(Temp!=Tail->GetNext())
    			{				
    				cout << Temp->GetData() << endl;
    				Temp = Temp->GetNext() ; 
    			}
    		}
    	}
    
    };
    
    int main()
    {
    	UseNodes A;
    	A.AddNode(29);
    	A.AddNode(7777);
    	A.Print();
    
    	getchar();
    }
    Thanks for any advice.

  2. #2
    Registered User SpaceCadet's Avatar
    Join Date
    Oct 2006
    Posts
    23
    The Node* Next member variable is not initialised by the constructor that receives an int argument.

    The GetNext function will therefore return an uninitialised pointer.

    Assigning Current = Head, leaks the new Node previously assigned to Current.

    I guess that the exception comes from the while loop because the GetNext function returns an uninitialised pointer and so you never leave the loop.

    Hope this helps....

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Where is your return 0?
    Code indentation needs adressing a bit

    You can get a better explaination of your seg fault if you are using MSVC++ as it will tell you the exact point of the unhandled exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2006, 05:46 PM
  2. I got an unhandled exception!!
    By LegendBreath in forum Game Programming
    Replies: 7
    Last Post: 04-19-2005, 01:55 PM
  3. unhandled exception error
    By trends in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 05:54 PM
  4. Unhandled exception
    By pdstatha in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2002, 06:03 PM
  5. Unhandled Exception
    By StringQuartet13 in forum C++ Programming
    Replies: 1
    Last Post: 03-04-2002, 04:18 PM

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