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?
#include <iostream>
usingnamespace 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++;
}
elseif (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++;
}
elseif (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.
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.