Not sure why Assertion fails

Here is the problem:
1
2
3
4
5
6
7
8
9
Assertion failed: ! Empty( ), file C:\Martin\MalikChapter7\corrupt2.cpp, line 56


This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 255 (0xFF)   execution time : 33.023 s
Press any key to continue.
 

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

  #include<iostream>
#include<cassert>
#include<stack>
using namespace std;

template <class T> class Stack {
   public:
        Stack ();			// constructor
      Stack (const Stack<T> & S);	// copy constructor
        ~ Stack ();			// destructor
        void Push (T item);
        T Pop ();
        bool Empty ();
        Stack & operator = (const Stack<T> & S); // assignment

   private:
        struct StackNode {
            T data;
            StackNode * next;
            };

        StackNode * top;
   };
//Constructor
template<class T>
Stack<T>::Stack()
{
    top = NULL;
}
template<class T>
Stack<T>::Stack (const Stack<T> & S)  //copy constructor
{
  S = NULL;
}
//Push
  //  . add given item to top of stack (front of list)

	template <class T>
	 void Stack<T>::Push(T item)
        {
          StackNode * tmp = new StackNode;
                tmp -> data = item;
                tmp -> next = top;
                top         = tmp;
        }
//(3) Pop
  //  . error if stack empty
  //  . save data from top-of-stack node
  //  . remove top node from stack (free storage)
  //  . return saved value

        template <class T> T Stack<T>::Pop( )
        {
            T retval;
            StackNode * tmp = top;

                  assert (! Empty( ));
                  retval = top -> data;
                  top    = top -> next;
                  delete tmp;
                  return (retval);
        }

//(4) Empty
  //  . return true iff stack is empty

        template <class T> bool Stack<T>::Empty( )
        {
           return (NULL == top);
        }
template<class T>
Stack<T>::~Stack()
{
//    delete [] StackNode;
    top = NULL;
}
template<class T>
Stack<T>& Stack<T>::operator= (const Stack<T> &S)
{
    if(this != &S)  //avoid self-copy
       // copyStack(S);
    return *this;
}  //end operator=

int main()
{
    Stack<int> myStack ;
    myStack.Push(1732);
    myStack.Push(1734);
    myStack.Push(1767);

    Stack<int> yourStack;
    yourStack = myStack;
    yourStack.Pop();
}
> assert (! Empty( ));
It asserts because the stack is empty, and you're trying to pop()

> yourStack = myStack;
Probably because you commented out this in your assignment operator.
// copyStack(S);

I have defined the copyStack function as it is in the textBook.
I get the following errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
C:\Martin\MalikChapter7\corrupt2.cpp:67:24: error: expected primary-expression before ';' token
     delete [] StackNode;
C:\Martin\MalikChapter7\corrupt2.cpp:70:15: error: expected unqualified-id before '=' token
     StackNode = new T[maxStackSize];
               ^
C:\Martin\MalikChapter7\corrupt2.cpp:71:5: error: 'assert' was not declared in this scope
     assert[StackNode != NULL];
     ^
C:\Martin\MalikChapter7\corrupt2.cpp:71:22: error: expected primary-expression before '!=' token
     assert[StackNode != NULL];
                      ^
C:\Martin\MalikChapter7\corrupt2.cpp:75:18: error: expected unqualified-id before '[' token
         StackNode[j] = otherStack.StackNode[j];
^

delete [] StackNode;

You call delete[] on a pointer that was returned from a call to new[]. StackNode is not a pointer. It's a class. You might as well write delete int;
I have defined another copy stack function. The program compiles OKAY!!!
but when I run it, it still gives an error on line 58 the assert is still failing.
1
2
Assertion failed: ! Empty( ), file C:\Martin\MalikChapter7\corrupt2.cpp, line 58
Well making something compile is just the easy part.

> I have defined another copy stack function. The program compiles OKAY!!!
Yeah, but did you bother to call it?

If you're not calling it at all, or it does the wrong thing, then your assignment operator does nothing and you're still looking down the barrel of the assert.

I forgot to post the new copyStack function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template<class T>
Stack<T>::Stack(const Stack<T> &other) {
Stack<T> *temp = other.top;
if (other.top != NULL) {

    Stack<T> *newnode = new Stack<T>;
    newnode = temp;

    while (temp->next != NULL) {

        temp = temp->next;
        newnode->next = temp;
    }
}
}
//(4) Empty
  //  . return true iff stack is empty

        template <class T> bool Stack<T>::Empty( )
        {
           return (NULL == top);
        }
template<class T>
Stack<T>::~Stack()
{
//    delete [] StackNode;
    top = NULL;
}
 
1. You need to be creating new StackNode's, not new Stacks.
2. You need to be creating them inside your while loop.

It would be better to use the Push() method you've already implemented.

Topic archived. No new replies allowed.