I am having troube getting this program to compile. I have been on it for the past 5 days and starting to feel burn out. maybe an extra set of eyes can point me in the right direction of what i'm doing wrong. The error messages i'm getting are: expected unqualified id before template; expected ';' before template. DynStack undeclared (first use this function), expect ';' before stack, and stack undeclared (first use this function).
Problem set: Write a template that will create a dynamic stack of any data type. Demonstrate the class with a driver program.
Here is my code so far: My header file:
//******************************************************************************
// Filename: DynStack.h *
// Programmer: Paul Williams *
// Date: March 4, 2011 *
// This is the Specification file *
// A class template for holding a linked list. *
// The node type is also a class template. *
//******************************************************************************
#ifndef DynStack_H
#define DynStack_H
//******************************************************************************
// The ListNode class creates a type used to store a node of the linked list. *
//******************************************************************************
template <class T>
class ListNode
{
public:
T value; // node value
ListNode<T> *next; // Pointer to the next node
// Constructor
ListNode (T nodeValue)
{ value = nodeValue;
next = NULL; }
}
//******************************************************************************
// DynStack class *
//******************************************************************************
template <class T>
class DynStack
{
private:
ListNode<T> *head; // List head pointer
public:
// Constructor
DynStack()
{ head = NULL; }
// Destructor
~DynStack();
// DynStack operations
void push(T);
void pop(T);
bool isEmpty();
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
template <class T>
DynStack<T>::~DynStack()
{
ListNode<T> *nodePtr // To traverse the list
ListNode<T> *nextNode // To point to the next node
// Position nodePtr at the head of the stack.
nodePtr = head;
// Traverse the list deleting each node.
while (nodePtr != NULL)
{
// Save a pointer to the next node
nextNode = nodePtr->next;
// Delete the current node
delete nodePtr;
// Position node pointer at the next node
nodePtr = nextNode;
}
}
//************************************************
// Member function push pushes the argument onto *
// the stack. *
//************************************************
template <class T>
void DynStack<T>::push(T newValue)
{
DynStack<T> *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new DynStack<T>;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node.
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else // Otherwise, insert NewNode before top.
{
newNode->next = head;
head = newNode;
}
}
//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************
template <class T>
void DynStack<T>::pop(T newValue)
{
DynStack *temp; // Temporary pointer
// First make sure the stack isn't empty.
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else // pop value off top of stack
{
num = head->value;
temp = head->next;
delete head;
head = temp;
}
}
//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************
template <class T>
bool DynStack<T>::isEmpty()
{
bool status;
if (!head)
status = true;
else
status = false;
return status;
}
};
#endif
Here is my .cpp file
#include <iostream>
#include "DynStack.h"
using namespace std;
//****************************************************
// Main function of the program *
//****************************************************
int main()
{
int catchVar; // To hold values popped off the stack
// Create a DynStack object.
DynStack stack;
// Push 5, 10, and 15 onto the stack.
cout << "Pushing 5\n";
stack.push(5);
cout << "Pushing 10\n";
stack.push(10);
cout << "Pushing 15\n";
stack.push(15);
// Pop the values off the stack and dispaly them.
cout << "Popping...\n";
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
// Try to pop another value off the stack.
cout << "\nAttempting to pop again... ";
stack.pop(catchVar);
system("pause");
return 0;
}