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
#include <iostream>
usingnamespace 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!
1) It depends. You can do it both ways
2) For a starter, in the constructor you are trying to do s_ptr = newchar(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.
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?
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.