Dynamic Stack Template

Apr 20, 2009 at 12:36am
I have the following at this point:

DynStack.h
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
// Specification file for the DynIntStack class
#ifndef DYNSTACK_H
#define DYNSTACK_H

template < class T >
class DynStack
{
private:
   // Structure for stack nodes
   struct StackNode
   {
      T value;        // Value in the node
      StackNode *next;  // Pointer to the next node
   };

   StackNode *top;      // Pointer to the stack top

public:
   // Constructor
   DynStack()
      {  top = 0; }

   // Destructor
   ~DynStack();

   // Stack operations
   void push( T );
   void pop( T & );
   bool isEmpty();
}; 
#endif 


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

//**************************************************
// Destructor                                      *
// This function deletes every node in the list.   *
//**************************************************

template < class T >
DynStack<T>::~DynStack()
{
   StackNode *nodePtr, *nextNode;

   // Position nodePtr at the top of the stack.
   nodePtr = top;

   // Traverse the list deleting each node.
   while (nodePtr != 0)
   {
      nextNode = nodePtr->next;
      delete nodePtr;
      nodePtr = nextNode;
   }
}

//************************************************
// Member function push pushes the argument onto *
// the stack.                                    *
//************************************************
template < class T >
void DynStack<T>::push( T num )
{
   StackNode *newNode; // Pointer to a new node

   // Allocate a new node and store num there.
   newNode = new StackNode;
   newNode->value = num;

   // If there are no nodes in the list
   // make newNode the first node.
   if (isEmpty())
   {
      top = newNode;
      newNode->next = 0;
   }
   else  // Otherwise, insert NewNode before top.
   {
      newNode->next = top;
      top = 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 &num )
{
   StackNode *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 = top->value;
      temp = top->next;
      delete top;
      top = temp;
   }
}

//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise.                     *
//****************************************************
template < class T >
bool DynStack<T>::isEmpty()
{
   bool status;

   if (!top)
      status = true;
   else
      status = false;

   return status;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "DynStack.h"
using namespace std;

int main()
{
	DynStack<int> intStack;

	intStack.push( 5 );

	return 0;
}


I'm getting the following errors and have no idea why. I'm sleep deprived and probably missing something stupid. Any help would be appreciated.

Error Log:
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall DynStack<int>::~DynStack<int>(void)" (??1?$DynStack@H@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DynStack<int>::push(int)" (?push@?$DynStack@H@@QAEXH@Z) referenced in function _main
Apr 20, 2009 at 1:19am
Template classes have to be completely implemented in the header. Move your implementation from the source to the header and you should be set.
Apr 30, 2009 at 2:29am
That did the trick. Thanks
Topic archived. No new replies allowed.