Problem with Templates

I have created stack class with templates in order to use it with many types. I created the main program to search a character. But compilation gives me errors I can't understand whats the problem. If anybody can help by looking at my code, I would be very much sincere.

StackX class
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 _STACKX_H
#define	_STACKX_H

template <typename T>
class StackX
{
private :
    T *stackArray;
    int maxSize;
    int top;
public:
    StackX(int s);
    ~StackX();
    void push(T j);
    T pop();
    bool isFull();
    bool isEmpty();
    T peak();
    int stackSize();
};



#endif	/* _STACKX_H */

Definition

Definition
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
#include"StackX.h"
#include<iostream>
using namespace std;

template <typename T>
StackX<T>::StackX (int s)
{
    top =-1;
    maxSize = s;
    stackArray = new T [maxSize];
}
template <typename T>
StackX<T>::~StackX()
{
    delete []stackArray;
}
template <typename T>
bool StackX<T>::isFull()
{
//    if ((maxSize-1)==top)
//        return true;
//    else
//        return false;
    return ((maxSize-1)==top);
}
template <typename T>
void StackX<T>::push(T j)
{
    if (isFull())
        cout<<"Stack is full"<<endl;
    else
    {
//        top++;
//        stackArray[top] = j;
        stackArray[++top] = j;
    }
}
template <typename T>
T StackX<T>::pop()
{
    if (isEmpty())
    {
        cout<<"Stack is empty"<<endl;
        return -99.0;//return value should be given becouse return type is given
    }
    else
        return stackArray[top--];
}
template <typename T>
T StackX<T>::peak()
{
    if (isEmpty())
    {
        cout<<"Stack is empty"<<endl;
        return -99.0;
    }
    else
        return stackArray[top];
}
template <typename T>
int StackX<T>::stackSize()
{
    return maxSize;
}
template <typename T>
bool StackX<T>::isEmpty()
{
    return top==-1;
}

Main Program
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
#include <stdlib.h>
#include"StackX.h"
#include<iostream>

void find(char,StackX<int> &);
using namespace std;
/*
 * 
 */

int main(int argc, char** argv)
{

    StackX<int> mystack(5);

    mystack.push('d');
    mystack.push('e');
    mystack.push('r');
    mystack.push('y');

    find('y',mystack);

    return (EXIT_SUCCESS);
}
void find(char c, StackX<int> & s)
{
    StackX<int> tempStack(s.stackSize());

    bool found = false;

    while (!s.isEmpty())
    {
        if (s.peak()==c)
        {
            found = true;
            break;
        }
    }
    tempStack.push(s.pop());
    while (!tempStack.isEmpty())
        s.push(tempStack.pop());
    if (found)
        cout<<"Value found"<<endl;
    else
        cout<<"Value not found"<<endl;
}
Template definitions AND implementations must be in the SAME file.

It is a "feature" - call it a glitch if you wish - of C++.

Other than that your implementation looks ok at a first glance.
Thank you for the help. That was the problem. I was learning templates from here and there I did not notice that one.
Topic archived. No new replies allowed.