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;
}

There are several problems with your code:

In Header:
Line 61 and 62: You are missing the semi-colon at the end of the line.

Line 28: You are missing a semi-colon after }

Line 52: There should be a closing bracket }; at the end of the DynStack declaration. And consequently, you should erase the closing bracket at line 148.

Lines 87, 90, and 116: I'm pretty sure that these should all be ListNode<T> and not DynStack.

Line 121: You need to add std:: in front of "cout" because the std namespace is not imported (and it should not be imported).

Line 91: It should be newValue instead of num.

Line 114: The parameter newValue should be passed-by-reference pop(T& newValue) and should probably be renamed. And so, the definition of pop at line 50 should be updated as well.

Line 125: It should be newValue instead of num.

Line 97: It should be "head" instead of "top".

Line 90: The class ListNode does not have a default constructor, so, you should pass the value "newValue" to its constructor, and that makes line 91 obsolete.

In CPP:
Line 14: You need to provide a type for the class template DynStack. It should be DynStack<int> With all the above changes, I got it to compile very easily. You have to develop a better eye for typos and things like that. These mistakes were trivial to see in one glimpse (for an experienced programmer).

To avoid this problem in the future, compile often.
Write the basic code framework first (includes, main(), return) and compile.
Write a class and compile. Make sure it compiles clean.
Write the next class and compile.
Write a function and compile.
Keep adding and compiling, fixing any errors immediately as you go.

Thanks a million for your help Mike_2000_17. I will make an effort to keen my eye for these type-o. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.