Template for a Stack?

Ok, Im editing this post.. I got a template to work, just confused as to the isFull and isEmpty functions..can someone explains how/ why this works.

To me, it doesn't see like it's telling you if its full or not..just returning a value everytime.

1
2
3
4
5
6
7
8
9
10
11
12
template <class T>	//Template isFull function
bool Stack<T> :: isFull()
{
	return top == size - 1;
}

template <class T>	//Template isEmpty function
bool Stack<T> :: isEmpty()
{
	return top == -1;
}
Last edited on
The next time write the compiler errors and the line you get them at. Right now I can see line 86
1
2
3
4
int main()
{
    Stack<int> test;
    //... 

When you create an instance of a template class you have to tell it the type of the template parameter, otherwise 'Stack' has no idea of what 'T' is. A less verbose way to do it is
1
2
typedef Stack<int> StackI;
StackI test;
Last edited on
UPDATE: Also, can someone tell me why this doesn't work..I cant figure it out for the life of me.

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
#include <iostream>
using namespace std;

template <class T> //Template for a stack
class Stack	//This is a stack class
{
	public:
		Stack(int);	//default constuctor, passed a int variable for size
		T pop();	//pop function
		void push (T element);	//push function
		int isEmpty();	//checks if stack is empty
		int currSize();	//returns the current size
		int isFull();	//checks if stack is full
		
	private:
		int size;	//holds size of stack
		int top;	//index of the top location of stack
		T *s_ptr;	//pointer holds a spot in memory
};

template <class T>	//Template constructor
Stack<T> :: Stack(int maxsize) //passed maxsize from initial declaration of stack
{
	size = maxsize;	//sets private size variable equal to size passed from declaration at main
	top = -1;	//initially there is no top, so set to -1
	s_ptr = new T(size);	//dynamically allocates memory to where pointer was stationed
}

template <class T> //Template push function
void Stack<T> :: push (T element)	//passed an element to push
{
	if (!isFull())	//if isFull function is not true (not full)
	{
		s_ptr[++top] = element; //go to next location and put the element in there
	}
	else
	{
		cout << "Stack is full!" << endl;
	}
}

template <class T>	//Template pop function
T Stack<T> :: pop ()
{
	if (!isEmpty()) // if isEmpty function is not true (not empty)
	{
		return s_ptr[top--]; // return the top location and then decrement top by 1
	}
	else
	{
		cout << "Stack is empty!" << endl;
	}
}

template <class T>	//Template isFull function
int Stack<T> :: isFull()
{
	if (top == size - 1)
	{
		return 1;
	}
	else 
	{
		return 0;
	}
}

template <class T>	//Template isEmpty function
int Stack<T> :: isEmpty()
{
	if (top == -1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

template <class T> 	//Template currSize function
int Stack<T> :: currSize ()
{
	return (top+1); //returns the actual size of stack at the point it is called
}


int main()
{
	Stack <char> word(10);
	char x;
	
	cout << "Enter a word and it will be reversed!" << endl << endl;
	cin >> x;
		
	while (x != '\0')
	{
		word.push(x);
		cin >> x;
	}
	
	
	while (!word.isEmpty())
	{
		cout << word.pop();
	}
	
	return 0;
}


1 - In main, should I arbitrarily assign a huge value to the stack, or should I make it so that it is based on how many inputs (letters) are typed by the user?

2- Why doesn't my main.cpp work.. I even tried hardcoding it!

Thanks.
1) It depends. You can do it both ways
2) For a starter, in the constructor you are trying to do s_ptr = new char(10) which will allocate memory for a single char and initialize it to 10. When you push an element for the second time you are trying to put it in s_ptr[1], but since you allocated memory only for one element, the program will try to write on invalid memory, causing a segmentation fault and a crash.
Also, I don't know of a way to input the null character from stdin, so I'm guessing your loop on line 96 would go on forever.
I thought Line 90 in main:

 
Stack <char> word(10);



would route to the constructor on Line 21:

1
2
3
4
5
6
7
template <class T>	//Template constructor
Stack<T> :: Stack(int maxsize) //passed maxsize from initial declaration of stack
{
	size = maxsize;	//sets private size variable equal to size passed from declaration at main
	top = -1;	//initially there is no top, so set to -1
	s_ptr = new T(size);	//dynamically allocates memory to where pointer was stationed
}


and create an array of chars with size 10... But your saying I am creating one spot and initializing it to 10? Are you sure?
Last edited on
Yes, to allocate an array on dynamic memory you have to use square brackets s_ptr = new T[size];.
Right now you're initializing a variable. Think what happens if T is a class instead of a char. You would be calling the costructor T(int). Primitive types can be initialized with brackets too, it's just uncommon to see it.

On a side note, you never deallocate the memory you allocate in the constructor. You should write a destructor too.
Your absolutely right.. I just realized I was using () instead of [ ] on s_ptr new T(size);

I got the program working finally with some other tweaks.. Thanks for you help....

Now, I have to write one that checks for palindromes....
Topic archived. No new replies allowed.