Unresovled External using Stack template

Hi all I've written this program to convert infix expressions to postfix. it works well for any expression however I wanted to optimize it using a stack template class so the program would work with any data type.
When I run it gives me this error:

InfrixtoPostifx.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack<char>::Stack<char>(void)" (??0?$Stack@D@@QAE@XZ) referenced in function "public: __thiscall IntoPost::IntoPost(void)"

it gives the same error for every function in the infixtopostfix class.

going to add class to eval postfix expression and would like to work with int/double which is why im using a template for stack

#include "Stack.h"
#include <stdlib.h>
#include <iostream>

using namespace std;

template <class StackType>
Stack<StackType>::Stack()
{
item = new StackType[size];

sTop = -1;
size = 1;
}

template <class StackType>
Stack<StackType>::Stack(int size)
{
item = new StackType[size];

if (item == NULL)
{
cout << "error problem initializing array!" << endl;
exit(1);
}

sTop = -1;
this->size = size;
}

template <class StackType>
Stack<StackType>::~Stack()
{
delete[] item;

item = NULL;
sTop = -1;
this->size = 0;
}

template <class StackType>
const StackType Stack<StackType>::top() { return item[sTop]; }

template <class StackType>
bool Stack<StackType>::isEmpty()
{
if (sTop == -1)
return true;
return false;
}

template <class StackType>
bool Stack<StackType>::isFull()
{
if (sTop == size - 1)
return true;
return false;
}

template <class StackType>
void Stack<StackType>::push(StackType ob)
{
if (isFull())
cout << "stack is full! cant push another item onto stack" << endl;
item[++sTop] = ob;
}

template <class StackType>
StackType Stack<StackType>::pop()
{
if (isEmpty())
cout << "Empty stack nothing to pop!" << endl;
StackType temp = item[sTop--];
return temp;
}

#include "InfixtoPostfix.h"

using namespace std;

IntoPost::IntoPost()
{
op = new Stack<char>();
post;
}

IntoPost::IntoPost(int size)
{
op = new Stack<char>(size);
post;
}

IntoPost::~IntoPost()
{
delete[] op;
op = NULL;
post.clear();
}

int IntoPost::priority(char op)
{
switch (op)
{
case '^': return 3; break;
case '*': return 2; break;
case '/': return 2; break;
case '+': return 1; break;
case '-': return 1; break;
default: return 0; break;
}
}

bool IntoPost::isOperator(char op)
{
char tmp = op;
if (tmp == '+' || tmp == '-' || tmp == '*' || tmp == '/' || tmp == '^')
return true;
return false;
}

string IntoPost::inToPost(string infix) //Assume string is in format 5 + 4 * 7
{
char ch;
for (int i = 0; i < infix.length(); i++)
{
ch = infix.at(i);
if (ch >= '0' && ch <= '9')
post.push_back(ch);
else if (ch == '(')
op->push(ch);
else if (isOperator(ch))
{
if (op->isEmpty() || priority(ch) > priority((char)op->top()))
op->push(ch);
else
{
while (!op->isEmpty() && priority(ch) <= priority((char)op->top()))
post.push_back(op->pop());
op->push(ch);
}
}
else if (ch == ')')
{
while (!op->isEmpty() && (char)op->top() != '(')
{
post.push_back(op->pop());
if (!op->isEmpty()) { op->pop(); } //to remove ( from stack
}
}
//else if (ch == ' ')
// i++;
}

while (!op->isEmpty())
{
post.push_back(op->pop());
}

return (string)post;
}

void IntoPost::print()
{
cout << post << endl;
}


Hello,
its hard to help you if your code is getting longer. It is even harder to help if it is not complete. How can I help you if I can not compile your code? ;)
But I have my good day today and cleaned up your example and it works!
So nothing more to do here.

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
#include <iostream>
#include <vector>
#include <cassert>


template <class StackType>
struct Stack {
    int sTop = -1;
    std::vector<StackType> items;

    Stack() = default;

    Stack(int size)
        : items(size)
    {
    }

    const StackType& top() const {
        return items[sTop];
    }

    bool isEmpty() const {
        return sTop == -1;
    }

    bool isFull() const {
        return sTop == items.size()-1;
    }

    void push(const StackType& value) {
        if (isFull()) {
            std::cout << "stack is full! cant push another item onto stack\n";
            exit(-1);
        }
        items[++sTop] = value;
    }

    StackType pop() {
        if (isEmpty()) {
            std::cout << "Empty stack nothing to pop!\n";
            exit(-1);

        }
        return items[sTop--];
    }
};

struct IntoPost {
    std::string post;
    Stack<char> stack;

    IntoPost(int size)
        : stack(size)
    {
    }

    int priority(char op) const {
        switch (op)
        {
        case '^': return 3; break;
        case '*': return 2; break;
        case '/': return 2; break;
        case '+': return 1; break;
        case '-': return 1; break;
        default: return 0; break;
        }
    }

    bool isOperator(char op) const {
        return op == '+' or op == '-' or op == '*' or op == '/' or op == '^';
    }

     //Assume string is in format 5 + 4 * 7
    std::string inToPost(std::string infix) {
        for(int i=0; i < infix.length(); i++) {
            char ch = infix.at(i);

            if (ch >= '0' and ch <= '9') {
                post.push_back(ch);
            } else if(ch == '(') {
                stack.push(ch);
            } else if (isOperator(ch)) {
                if (stack.isEmpty() or priority(ch) > priority(stack.top())) {
                    stack.push(ch);
                } else {
                    while(not stack.isEmpty() and priority(ch) <= priority(stack.top())) {
                        post.push_back(stack.pop());
                    }
                    stack.push(ch);
                }
            } else if(ch == ')') {
                while (not stack.isEmpty() and stack.top() != '(') {
                    post.push_back(stack.pop());
                    if (not stack.isEmpty()) {
                        stack.pop(); //to remove ( from stack
                    }
                }
            }
            //else if (ch == ' ')
            // i++;
        }

        while(not stack.isEmpty()) {
            post.push_back(stack.pop());
        }

        return post;
    }
};

int main() {
    std::string s = "5 + 4 * 7";
    std::cout << "converting '" << s << "'\n";

    int stackSize = 10;
    IntoPost converter(stackSize);
    std::string post = converter.inToPost(s);

    std::cout << post << "\n";
    assert(post == "547*+");
}
Hi Thomas thanks for the reply I could post the rest of the code but it was working fine before I added the template to the stack class.


I can post the rest of the code if you want. I Really thank you for your reply but unfortunately im unable to use vectors I HAVE to implement stacks as an array
Topic archived. No new replies allowed.