not quite sure if anybody will get to this on time because i have class in a couple hours.but ill give it a try
thnx for the help, but now i have a NEW compile error. ive had this compile error before but not for the same reason. For my last program i forgot to link my implementation with my main. but i believe im getting the error now due to something wrong with my templates.
Header
CODE
template <class Type>
class stackType
{
public:
void initializeStack(); //Function to initialize the stack to an empty state.
bool isEmptyStack() const; //Function to determine whether the stack is empty.
bool isFullStack() const;//Function to determine whether the stack is full
void destroyStack(); //Function to destroy current stack
void push(const Type& newItem); //Functions to add newItem to the stack
Type top() const; //Function to return the top element of the stack
void pop(); // Function to remove the top element of the stack
stackType(int stackSize = 25); //constructor
~stackType(); //destructor
private:
int maxStackSize; //variable to store the maximum stack size
int stackTop; //variable to point to the top of the stack
Type *list; //pointer to the array that holds the stack elements
};
implementation
CODE
#include "stackADT.h"
#include <iostream>
using namespace std;
template <class Type>
void stackType<Type>::initializeStack()
//Function to initialize the stack to an empty state.
//Precondition:
//Postcondition: stackTop= 0
{
stackTop = 0;
}
template <class Type>
bool stackType<Type>::isEmptyStack() const
//Function to determine whether the stack is empty.
//Precondition:
//Postcondition:Returns true if the stack is empty, otherwise
// returns false
{
return(stackTop == 0);
}
template <class Type>
bool stackType<Type>::isFullStack() const
//Function to determine whether the stack is full
//Precondition:
//Postcondition:Returns true if the stack is full, otherwise
// returns false
{
return )stackTop == maxStackSize);
}
template <class Type>
void stackType<Type>::destroyStack()
//Function to destroy current stack
//Precondition:
//Postcondition:
{
stackTop = 0;
}
template <class Type>
void stackType<Type>::push(const Type& newItem)
//Functions to add newItem to the stack
//Precondition: The stack exists and is not full
//Postcondition: The stack is changed and newItem
// is added to the top of the stack
{
if (!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cout<<"Cannot add to a full stack."<<endl;
}
template <class Type>
Type stackType<Type>::top() const
//Function to return the top element of the stack
//Precondition: The stack exists and is not empty
//Postcondition: If stack is empty, program terminates
// , otherwise top element of stack is returned
{
assert(stackTop != 0);
return list[stackTop - 1];
}
template <class Type>
void stackType<Type>::pop()
// Function to remove the top element of the stack
//Precondition: Stack exists and is not empty
//Postcondition: Stack is changed and top element is
// removed from stack
{
if (!isEmptyStack())
stackTop--;
else
cout<<"Cannot remove from an empty stack."<<endl;
}
template <class Type>
stackType<Type>::stackType(int stackSize)
//constructor
//Postcondition: Variable list contains base address
// of array, stacktop = 0, maxStackSize = stackSize.
{
if (stackSize<=0)
{
cout<<"Size of the array to hold the stack must be positive."<<endl;
cout<<"Creating an array of size 25."<<endl;
maxStackSize = 25;
}
else
maxStackSize = stackSize;
stackTop = 0;
list = new Type[maxStackSize];
}
template <class Type>
stackType<Type>::~stackType()
//destructor
//Postcondition: the array (list) holding the stack elements is deleted.
{
delete [] list;
}
Main
CODE
#include "stackADT.h"
#include <iostream>
using namespace std;
int main()
{
stackType<int> stack(25); //declare stack size
stack.initializeStack(); //creates empty stack
stack.push(23); //add 23 to stack
stack.push(45); //add 45 to stack
stack.push(38); //add 38 to stack
cout<<"The elements of stack: "; //display elements of stack
while (!stack.isEmptyStack())
{
cout<<stack.top()<<" ";
stack.pop();
}
cout<<endl;
if(!stack.isEmptyStack())
cout<<"The original stack is not empty."<<endl;
cout<<"The top element of the stack: "<<stack.top()<<endl;
return 0;
}
this is the error im getting, i heard its a common mistake to get these lnk2001 errors with templates
CODE
tackADTmain.obj : error LNK2001: unresolved external symbol "public: __thiscall stackType<int>::~stackType<int>(void)" (??1?$stackType@H@@QAE@XZ)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: void __thiscall stackType<int>::pop(void)" (?pop@?$stackType@H@@QAEXXZ)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: int __thiscall stackType<int>::top(void)const " (?top@?$stackType@H@@QBEHXZ)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: bool __thiscall stackType<int>::isEmptyStack(void)const " (?isEmptyStack@?$stackType@H@@QBE_NXZ)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: void __thiscall stackType<int>::push(int const &)" (?push@?$stackType@H@@QAEXABH@Z)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: void __thiscall stackType<int>::initializeStack(void)" (?initializeStack@?$stackType@H@@QAEXXZ)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: __thiscall stackType<int>::stackType<int>(int)" (??0?$stackType@H@@QAE@H@Z)
Debug/stackADTImplementation.exe : fatal error LNK1120: 7 unresolved externals
This post has been edited by visionz: 29 Oct, 2007 - 09:54 AM