generic stack with previous functions

Im trying to implement a generic stack using previous functions from a different program.

Im getting these errors.
F:\373\L_Stack.cpp In destructor `Stack<StackItem>::~Stack()':
43 F:\373\L_Stack.cpp expected template-name before '<' token
43 F:\373\L_Stack.cpp expected primary-expression before '>' token
43 F:\373\L_Stack.cpp expected primary-expression before ')' token
F:\373\L_Stack.cpp In constructor `Stack<StackItem>::Stack() [with StackItem = int]':
52 F:\373\L_Stack.cpp instantiated from here
10 F:\373\L_Stack.cpp no match for 'operator=' in '((Stack<int>*)this)->Stack<int>::Top = 0'
F:\373\L_Stack.cpp In member function `void Stack<StackItem>::Push(StackItem) [with StackItem = int]':
52 F:\373\L_Stack.cpp instantiated from here
15 F:\373\L_Stack.cpp 'struct Stack<int>::NodeType' has no member named 'ListEmpty'
17 F:\373\L_Stack.cpp 'struct Stack<int>::NodeType' has no member named 'FirstInsert'
21 F:\373\L_Stack.cpp 'struct Stack<int>::NodeType' has no member named 'FrontInsert'
F:\373\L_Stack.cpp In member function `void Stack<StackItem>::Pop(StackItem&) [with StackItem = int]':
52 F:\373\L_Stack.cpp instantiated from here
27 F:\373\L_Stack.cpp 'struct Stack<int>::NodeType' has no member named 'FirstItem'
28 F:\373\L_Stack.cpp 'struct Stack<int>::NodeType' has no member named 'Delete'
F:\373\L_Stack.cpp In member function `StackItem Stack<StackItem>::TopItem() [with StackItem = int]':
52 F:\373\L_Stack.cpp instantiated from here
33 F:\373\L_Stack.cpp request for member `FirstItem' in `((Stack<int>*)this)->Stack<int>::Item->Stack<int>::NodeType::Item', which is of non-class type `int'
F:\373\L_Stack.cpp In member function `bool Stack<StackItem>::IsEmpty() [with StackItem = int]':
52 F:\373\L_Stack.cpp instantiated from here
38 F:\373\L_Stack.cpp request for member `ListEmpty' in `((Stack<int>*)this)->Stack<int>::Item->Stack<int>::NodeType::Item', which is of non-class type `int'

header:
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
#ifndef L_Stack_h_
#define L_Stack_h_
#include "LList.h"
#include <iostream>
template<class StackItem>
class Stack
{
      template <typename t>
      friend std::ostream& operator<<(std::ostream & ,const Stack<t> &);
      private:
              struct NodeType
              {
                     StackItem Item;
                     NodeType *Next;
              };
              NodeType *Item,Top;
      public:
             Stack();
             void Push(StackItem);
             void Pop(StackItem &);
             StackItem TopItem();
             bool IsEmpty();
             ~Stack();

};
#endif 


implementation:
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
#include "L_Stack.h"
#include "LList.h"
#include<iostream>
using namespace std;


template<class StackItem>
Stack<StackItem>::Stack()
{
   Top=NULL;
}
template<class StackItem>
void Stack<StackItem>::Push(StackItem x)
{
     if (Item->ListEmpty())
     {
        Item->FirstInsert(x);
     }
     else 
     {
          Item->FrontInsert(x);
     }
}
template<class StackItem>
void Stack<StackItem>::Pop(StackItem &x)
{
     x=Item->FirstItem();
     Item->Delete();
}
template<class StackItem>
StackItem Stack<StackItem>::TopItem()
{
 return Item->Item.FirstItem;
}
template<class StackItem>
bool Stack<StackItem>::IsEmpty()
{
     return Item->Item.ListEmpty;
}    
template<class StackItem>
Stack<StackItem>::~Stack()
{
    Item.~LList<StackItem>();
}

template <class t> 
ostream& operator<<(ostream &output,Stack<t> const&s)
{          
           output<<s.Item;
           return output;
}
template class Stack<int>;
template ostream& operator<< (ostream &output, Stack<int> const&l);


if it helps the program with the functions im supposed to be using
implementation
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "LList.h"
#include<iostream>
using namespace std;
template <class itemtype>
llist<itemtype>::llist()
{  
             
   first=last=NULL;
}
template <class itemtype>
void llist<itemtype>::FirstInsert(itemtype x)
{
     first= new nodetype;
     last= first;
     first->item =x;
     first->next = NULL;
}
template <class itemtype>
void llist<itemtype>::FrontInsert(itemtype x)
{
     nodetype *newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=first;
     first=newnode;
}
template <class itemtype>
void llist<itemtype>::EndInsert(itemtype x)
{
     nodetype *newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=NULL;
     last->next=newnode;
     last=newnode;
}
template <class itemtype>
void llist<itemtype>::InOrderInsert(itemtype x)
{
     nodetype *cur;
     nodetype *prev;
     
     nodetype *newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=NULL;
     if(x<=first->item)
     {
       newnode->next=first;
       first=newnode;
     }
     else if (x>=last->item)
     {
      last->next=newnode;
     last=newnode;
     }
     else
     {
         prev=first;
         cur=first->next;
         while(x>cur->item)
         {
          prev=cur;
          cur=cur->next;
          }
     newnode->next=cur;
     prev->next=newnode;
     }
}
template <class itemtype>
bool llist<itemtype>::ListEmpty()
{
return(first==NULL);
}
template <class itemtype>
itemtype llist<itemtype>::FirstItem()
{
return first->item;
}
template <class itemtype>
itemtype llist<itemtype>::LastItem()
{
return last->item;
}
template <class itemtype>
bool llist<itemtype>::Search(itemtype x)
{
     nodetype *cur;
     cur=first;
     while ((cur!=NULL)&&(x!=cur->item))
     {
           cur=cur->next;
     }
     return(cur!=NULL);
}
template <class itemtype>
llist<itemtype>::~llist()
{
     nodetype *delnode;
     while(first!=NULL)
     {
       delnode=first;
       first=first->next;
       delete delnode;
     }
}
template <class itemtype>
bool llist<itemtype>::Delete (itemtype x)
{
     nodetype *cur, *prev;
     nodetype *delnode;
     prev=NULL;
     cur=first;
      while ((cur!=NULL)&&(x!=cur->item))
     { 
            prev=cur;
            cur=cur->next;
            if(cur==NULL)
              return 0;
            else
                delnode=cur;
                if(prev==NULL)
                {   
                    first=first->next;
                }
                else
                {
                prev->next=cur->next;
                if (cur==last)
                   last=prev;
                }
                delete delnode;
                return 1;
     }
}

template <class t> 
ostream& operator<<(ostream &output,llist<t> const&l)
{          

        typename llist<t>::nodetype *cur;
        cur=l.first;
        while(cur!=NULL)
        {
           output<<cur->item<<"|";
           cur=cur->next;
           }
        return output;
}
template class llist<int>;
template class llist<char>;

template ostream& operator<< (ostream &output, llist<int> const&l);
template ostream& operator<< (ostream &output, llist<char> const&l);


any help is appreciated
Last edited on
NodeType is a struct with no member functions.
Item->FirstInsert(x);

put definition of templates in header files only.
dont call destructor, it will be called automatically.


Topic archived. No new replies allowed.