Unhandled Exception (Vectors)

This is a discussion on Unhandled Exception (Vectors) within the C++ Programming forums, part of the General Programming Boards category; I get and Unhandled Exception error when running the following code snippit below. It works without the nested class but ...

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Unhandled Exception (Vectors)

    I get and Unhandled Exception error when running the following code snippit below. It works without the nested class but I wanted the inplementation to be closer to the requirement specs. Only part of the code is provided below. I would be happy to paste more if it is necessary.

    I traced the error with debug, thinking it was the variable being passed, but any vector functions, like size, empty, etc.. cause it to crash so the value has nothing to do with it. Its like the vector cannot be accessed at all.

    Furthermore, the program started working for one brief time and then would stop if tested again, or work half-way and then fail. I cannot see where the memory leak is?

    Code:
    
    template <class Elem, int size>
    class Bag  {
    
    template <class Elem>
    	class StackClass  {  // Nested class withing Bag class
    		friend class Bag <Elem, size>;
    	
    
    	private:
    		vector<Elem> *sVector;
            
    		const bool Push (const Elem &pushValue) const  {
    			cout << "\n********Pushing************\n";
                              // Note:  ERROR OCCURS WITH ANY VECTOR FUNCTION BELOW:
    			cout << endl << sVector->size() << endl;
    			sVector->push_back (pushValue);
                            return true;  }
    };  // ************* end of Stack class definition *********
    
    private:
    		Elem *contents;  // pointer array of type: Elem
    
    public:
    		Bag();
    		~Bag();
    		StackClass<Elem> *Stack;
    
    };  // end class Bag definition
    
    
    template <class Elem, int size>
    Bag<Elem, size>::Bag() {        // Constructor
    	
    	StackClass<Elem> *Stack = new StackClass<Elem>;
    	contents = new Elem [size];
    	Stack->sVector = new vector<Elem>;
    }
    
    template <class Elem, int size>
    const bool Bag<Elem, size>::Grab (int &item) { 
    	int count;
    
            if (!Stack->Push (contents [count]))
    	     cout << "\n***** Stack Error, Push Failed !! *****\n\n";
    			}  // end push
    	return true;
    
    }  // end Grab

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    What's the overall goal of the project and why do you want a pointer to a vector?

    vector<Elem> *sVector; //pointer to a vector???

    If you are trying to make a two dimensional container I'd use a vector of vectors and avoid use of pointers/new, etc.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Code:
    template <class Elem, int size>
    const bool Bag<Elem, size>::Grab (int &item) { 
    	int count;
    
            if (!Stack->Push (contents [count]))
    	     cout << "\n***** Stack Error, Push Failed !! *****\n\n";
    			}  // end push
    	return true;
    
    }  // end Grab
    Here you are using count without initializing it unless you just didn't post the initialization code.

  4. #4
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Found it!

    These crazy errors can be such a mystery. Anyway, it started working after I changed the following BAD CODE:


    Code:
    public:
    	StackClass<Elem> *Stack;
    
    };  // end class Bag definition
    
    
    template <class Elem, int size>
    Bag<Elem, size>::Bag() {        // Constructor
    	
    	StackClass<Elem> *Stack = new StackClass<Elem>;
    }
    (I guess I defined the stack pointer twice and instead of just referencing it) GOOD CODE:


    Code:
    public:
    		StackClass<Elem> *Stack;
    
    };  // end class Bag definition
    
    
    template <class Elem, int size>
    Bag<Elem, size>::Bag() {        // Constructor
    	
    	Stack = new StackClass<Elem>;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging and unhandled exception
    By Overlord in forum Windows Programming
    Replies: 2
    Last Post: 07-25-2008, 07:39 AM
  2. Replies: 3
    Last Post: 11-11-2006, 06:46 PM
  3. Debugger: Unhandled Exception: User Breakpoint
    By JaWiB in forum Windows Programming
    Replies: 1
    Last Post: 12-18-2005, 05:46 PM
  4. unhandled exception error
    By trends in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 06:54 PM
  5. Unhandled Exception
    By StringQuartet13 in forum C++ Programming
    Replies: 1
    Last Post: 03-04-2002, 05:18 PM

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