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