What is wrong with this template?
-
StackA.h
#ifndef STACKA_H #define STACKA_H // ********************************************************* // Header file StackA.h for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackException.h" #include const int MAX_STACK = 100; template < class StackItemType > class Stack { public: // constructors and destructor: Stack(); // default constructor // stack operations: bool isEmpty() const; void push(StackItemType newItem) throw(StackException); void pop() throw(StackException); void pop(StackItemType& stackTop) throw(StackException); void getTop(StackItemType& stackTop) const throw(StackException); private: StackItemType items[MAX_STACK]; // array of stack items int top; // index to top of stack }; // end class #endif
StackA.cpp// ********************************************************* // Implementation file StackA.cpp for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackA.h" // Stack class specification file #include "StackException.h" template < class StackItemType > Stack::Stack(): top(-1) { } // end default constructor template < class StackItemType > bool Stack::isEmpty() const { return top < 0; } // end isEmpty template < class StackItemType > void Stack::push(StackItemType newItem) throw(StackException) { // if stack has no more room for another item if (top >= MAX_STACK-1) throw StackException("StackException: stack full on push"); else { ++top; items[top] = newItem; } // end if } // end push template < class StackItemType > void Stack::pop() throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else --top; // stack is not empty; pop top } // end pop template < class StackItemType > void Stack::pop(StackItemType& stackTop) throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else { // stack is not empty; retrieve top stackTop = items[top]; --top; // pop top } // end if } // end pop template < class StackItemType > void Stack::getTop(StackItemType& stackTop) const throw(StackException) { if (isEmpty())
-
StackA.h
#ifndef STACKA_H #define STACKA_H // ********************************************************* // Header file StackA.h for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackException.h" #include const int MAX_STACK = 100; template < class StackItemType > class Stack { public: // constructors and destructor: Stack(); // default constructor // stack operations: bool isEmpty() const; void push(StackItemType newItem) throw(StackException); void pop() throw(StackException); void pop(StackItemType& stackTop) throw(StackException); void getTop(StackItemType& stackTop) const throw(StackException); private: StackItemType items[MAX_STACK]; // array of stack items int top; // index to top of stack }; // end class #endif
StackA.cpp// ********************************************************* // Implementation file StackA.cpp for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackA.h" // Stack class specification file #include "StackException.h" template < class StackItemType > Stack::Stack(): top(-1) { } // end default constructor template < class StackItemType > bool Stack::isEmpty() const { return top < 0; } // end isEmpty template < class StackItemType > void Stack::push(StackItemType newItem) throw(StackException) { // if stack has no more room for another item if (top >= MAX_STACK-1) throw StackException("StackException: stack full on push"); else { ++top; items[top] = newItem; } // end if } // end push template < class StackItemType > void Stack::pop() throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else --top; // stack is not empty; pop top } // end pop template < class StackItemType > void Stack::pop(StackItemType& stackTop) throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else { // stack is not empty; retrieve top stackTop = items[top]; --top; // pop top } // end if } // end pop template < class StackItemType > void Stack::getTop(StackItemType& stackTop) const throw(StackException) { if (isEmpty())
-
StackA.h
#ifndef STACKA_H #define STACKA_H // ********************************************************* // Header file StackA.h for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackException.h" #include const int MAX_STACK = 100; template < class StackItemType > class Stack { public: // constructors and destructor: Stack(); // default constructor // stack operations: bool isEmpty() const; void push(StackItemType newItem) throw(StackException); void pop() throw(StackException); void pop(StackItemType& stackTop) throw(StackException); void getTop(StackItemType& stackTop) const throw(StackException); private: StackItemType items[MAX_STACK]; // array of stack items int top; // index to top of stack }; // end class #endif
StackA.cpp// ********************************************************* // Implementation file StackA.cpp for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackA.h" // Stack class specification file #include "StackException.h" template < class StackItemType > Stack::Stack(): top(-1) { } // end default constructor template < class StackItemType > bool Stack::isEmpty() const { return top < 0; } // end isEmpty template < class StackItemType > void Stack::push(StackItemType newItem) throw(StackException) { // if stack has no more room for another item if (top >= MAX_STACK-1) throw StackException("StackException: stack full on push"); else { ++top; items[top] = newItem; } // end if } // end push template < class StackItemType > void Stack::pop() throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else --top; // stack is not empty; pop top } // end pop template < class StackItemType > void Stack::pop(StackItemType& stackTop) throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else { // stack is not empty; retrieve top stackTop = items[top]; --top; // pop top } // end if } // end pop template < class StackItemType > void Stack::getTop(StackItemType& stackTop) const throw(StackException) { if (isEmpty())
All those template functions (in StackA.cpp) used by the templated class must be in the header file not in a seperate CPP file. Otherwise the compiler only has the prototypes for functions it has not generated. Therefore, you will get "unresolved external...", because they do not exist. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen