.h file is giving me issues

I'm writing a program to convert infix to postfix notation. For some weird reason my 'list.h' file keeps giving me this error "error C2953: 'List': class template has already been defined" then the error line takes me to the last line of the code the '};'. Can someone educate me as to what is wrong with this?
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
#include <iostream>

using namespace std;

template <typename LIST>
class List
{
private:
     typedef LIST L;
	 int sz, pos;
	 int MAX = 50;
      L ary[50];
	 void getsize(int i)
	 { sz = i; }
	 
public:
     List()
     {  sz = 0, pos = 0; }

	 
	 // Constructors.
     List(List& n)
     {
	     int j, k;
	     sz = n.size();
	     pos = n.getPos();
	     n.setPos(0);
	     k = 0;
	
	     while(k < j - 1 && k < MAX)
	     {
		     n.setPos(k);
		     ary[k] = n.getElement();
		     k++;
	     }
	     return;
     }

     // Modifier methods.
     // setPos(int) places current position in a certain position in the list
     void setPos(int i)
     {
          if (i >= 0 && i < sz)
               pos = i;
     }

     void first()
     {
          if (sz > 0)
               pos = 0;
     }

     void last()
     {
          if (sz > 0)
               pos = sz - 1;
     }

     void prev()
     {
	     if (sz > 1)
		     if (pos == 0)
			     return;
         pos--;
     }

     void next()
     {
	     if (pos < (sz - 1))
		     pos++;
     }

     void insertBefore(L val)
     {
	     if (sz == 0)
	     {
		     pos = 0;
             ary[pos] = val;
             sz++;
         }
          else if (sz < MAX)
          {
             for (int i = sz; i > pos - 1; i--)
			     ary[i] = ary[i - 1];
		     ary[pos] = val;
		     sz++;
          }
     }
     // insertAfter inserts a new element after the current position
     void insertAfter(L val)
     {
          if (sz == 0)
          {
               pos = 0;
               ary[pos] = val;
               sz++;
          }
          else if (sz < MAX)
          {
               for (int i = sz; i > pos; i--)
                    ary[i] = ary[i - 1];
               ary[pos + 1] = val;
               sz++;
               pos++;
          }
     }

     // erase deletes the current element
     void erase()
     {
          ary[pos] = 0;
          for (int i = pos; i < sz - 1; i++)
               ary[i] = ary[i + 1];
          sz--;
     }

     // clears the list of data
     void clear()
     {
          for (int i = 0; i < MAX; ++i)
               ary[i] = 0;
          sz = 0;
          pos = -1; //THIS COULD BE AN ERROR CHANGE TO 0 maybe
     }

     // Accessor methods.
     // size returns the size of the list (number of elements in list)
     int size() const
     {  return sz;}
     // getPos returns current position or where you are in the list
     int getPos() const
     {  return pos;}

     // get Element returns the one element that current position is pointing to
     L getElement() const
     { return ary[pos];}

     // empty returns true or false if list is empty or not.
     bool empty() const
     {   return sz == 0; }

};

I went around the .h and included <list> just to make the program run so i could get an output. And it's all messed up. Unsure why or what is going on with the output. The output is reversed in format and is not printing the 'postfix' of the input.

Here's is the .cpp file.
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
//#include "list.h"
#include <list>
#include "stack.h"
using namespace std;

template <typename que>
class Queue
{
private:
     typedef que Q;
     List<Q> MyQueue;

public:

     void enqueue(Q i)
     {
          MyQueue.last();
          MyQueue.insertAfter(i);
     }

     Q dequeue()
     {
          MyQueue.pop();
          return MyQueue.top();
     }

     Q front()
     {
          MyQueue.last();
          return MyQueue.getElement();
     }

     bool empty()
     { return MyQueue.empty(); }

     int size()
     { return MyQueue.size();  }
};

//Class Variables and Function Prototypes
Stack<char> operation;
Stack<int> OpPriority;
Stack<float> num;
Queue<char> postfix;
int Result(char);
int inFixPri(char);
int StackPri(char);
void Conversion(char);
float Operation(float, float, char);

int inFixPri(char k)
{
     if (k == '$')
          return 0;
     if (k == '+' || k == '-')
          return 1;
     if (k == '*' || k == '/')
          return 2;
     if (k == '(')
          return 3;
     if (k == '^')
          return 4;
}

int StackPri(char k)
{
     if (k == '$')
          return 0;
     if (k == '+' || k == '-')
          return 1;
     if (k == '*' || k == '/')
          return 2;
     if (k == '(')
          return 3;
     if (k == '^')
          return 4;
}

void Conversion(char k)
{
     char pr;//previous value
     int p;//priority
     int popP;//pop priority
     if (k != ' ')
     {
          popP = inFixPri(k);
          pr = ' ';
          while (OpPriority.top() >= popP && operation.top() != '$' && !(operation.top() == '(' && k != ')' && pr != '(' ))
          {
               if (!(operation.top() == '$' && operation.top() == '(' && operation.top() == ')'))
               {
                    if (operation.top() != '(')
                         postfix.enqueue(operation.top());
               }
               else
                    popP = -2;
               if (operation.top() != '(' || k == ')')
               {
                    pr = operation.top();
                    operation.pop();
                    OpPriority.pop();
               }
           }
          if (k != '$' && k != ')')
          {
               if (popP == -1)
                    postfix.enqueue(k);
               else
               {
                    operation.push(k);
                    p = StackPri(k);
                    OpPriority.push(p);
               }
          }
     }
}

float Operation(float i, float j, char k)
{
     float h;
     if (k == '+')
          h = i + j;
     if (k == '-')
          h = i - j;
     if (k == '/')
          h = i / j;
     if (k == '*')
          h = i * j; 
     if (k == '^')
          h = pow(i, j);
     return h;
}

int Result(char k)
{
     float t = 0;//total
     float n;//number
     float a, b;//numbers
     int p;//priority

     if (k != '$')
     {
          p = StackPri(k);
          if (p == 0)
          {
               n = k - '0';
               num.push(n);
          }
          else
          {
               a = num.top();
               num.pop();
               b = num.top();
               num.pop();
               num.push(Operation(a, b, k));
          }
          if (postfix.size() - 1 == 0)
          {
               t = num.top();
               num.pop();
          }
     }
     return t;
}

int main()
{
     //ofstream out("postfix.out");
     ifstream inf("postfix.in");
     
     int c, e;//count and ending variable
     float t;//total
     string i, d;//infix and data variable
     cout << "Infix to Postfix Program\n";
     operation.push('$');//Pushes $ onto stack
     OpPriority.push(0);

     while (inf >> d)
     {
          getline(inf, i);
          i += d;
          
          cout << "Infix: " << i << " = " << "\n\n";
          i = i + '$';
          e = i.length();

          for (c = 0; c < e; c++)
               inFixPri(i.at(c));
          t = 0; 
          while (postfix.size())
          {
               cout << "Postfix: " << postfix.front() << "  ";
               t = t + Result(postfix.front());
               postfix.empty();
          }
          cout << " = " << t << endl;
     }
     return 0;
}


Here is the 'stack.h'
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
#include <iostream>
#include "list.h"
using namespace std;

template <typename STK>
class Stack
{
private:
     typedef STK S;
     List<S> MyStack;

public:
		
S top()
{
     MyStack.last();
     return MyStack.getElement();
}

void clear()
{ MyStack.clear();}

void push(S i)
{
     MyStack.last();
     MyStack.insertAfter(i);
}

void pop()
{
     MyStack.erase();
     MyStack.getElement();
}

bool empty()
{ return MyStack.empty();}

int size()
{ return MyStack.size();}

};


INPUT:
1
2
3
4
5
6
7
8
9
10
11
5 + 7
4 ^ 2 + 1
3 * 8 + 6
5 ^ 4 - 6 ^ 4
5 -  (3 * 7 + 3) / 4 - 9
8 * ( 9 / 3 - 2 ) / 4 + 8 * 6
5 ^ 3 * 4 + ( 2 + ( 9 * 8 / ( 2 * 6 * ( 8 / 4 ) ) ) ^ 2 * 8 - 5 ) / 5 ^ 2 - 9
5 - 3 * 8 / 6 ^ 3
3 ^ 3 ^ 2 *3
8 * ( 6 / 3 - 1 ) / 6 + 5 * 6 +3
( ( ( ( ( 8 * 7 ) ) ) ) )


OUTPUT:
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
Infix to Postfix Program
Infix:  + 75 =

 = 0
Infix:  ^ 2 + 14 =

 = 0
Infix:  * 8 + 63 =

 = 0
Infix:  ^ 4 - 6 ^ 45 =

 = 0
Infix:  -  (3 * 7 + 3) / 4 - 95 =

 = 0
Infix:  * ( 9 / 3 - 2 ) / 4 + 8 * 68 =

 = 0
Infix:  ^ 3 * 4 + ( 2 + ( 9 * 8 / ( 2 * 6 * ( 8 / 4 ) ) ) ^ 2 * 8 - 5 ) / 5 ^ 2 - 95 =

 = 0
Infix:  - 3 * 8 / 6 ^ 35 =

 = 0
Infix:  ^ 3 ^ 2 *33 =

 = 0
Infix:  * ( 6 / 3 - 1 ) / 6 + 5 * 6 +38 =

 = 0
Infix:  ( ( ( ( 8 * 7 ) ) ) ) )( =

 = 0
Last edited on
> 'List': class template has already been defined
header guards
1
2
3
4
5
#pragma once
#ifndef SOME_UNIQUE_IDENTIFIER
#define SOME_UNIQUE_IDENTIFIER
//file content
#endif 


for the rest, too many files, upload to github
From when you designed this (and you did design before starting to code?), how did you expect it to work? Did you try your design using pen and paper with example input? If the design produced the expected output from a given input, then the error is with the conversion of the design to the code. This is when the debugger comes into its own. Trace through the code to see where it deviates from what expected from the design - and you have found the issue. If you idn't design and just started to code........ then still use the debugger to see where the code isn't doing what is expected. Then you either need to fix the code or change the design - depending upon the issue found.
Topic archived. No new replies allowed.