Im new to programming, in our c++ course, we have to build a stack class and using template, I followed the book and tried to put it together but still a lot of error. So I hope the experts here can help point me to the right direction.
StackType.h:
class FullStack
{};
class EmptyStack
{};
template<class ItemType>
class StackType
{
public:
StackType(int max);
/*
* Function: constructor
* Precondition: none
* Postcondition: Stack has been initialized
*/
bool IsEmpty() const;
/*
* Function: Determines whether the stack is empty
* Precondition: Stack has been initialized
* Postcondition: Function value = (stack is empty)
*/
bool IsFull() const;
/*
* Function: Determines whether the stack is full
* Precondition: Stack has been initialized
* Postcondition: Function value = (stack is full)
*/
void Push(ItemType item);
/*
* Function: Add new item to the top of the stack
* Precondition: Stack has been initialized
* Postcondition: If (stack is full), exception FullStack is thrown,
* else new item is at the top of the stack
*/
void Pop();
/*
* Function: Remove top item from the stack
* Precondition: Stack has been initialized
* Postcondition: If (stack is empty), exception EmptyStack is thrown,
* else top item has been removed from stack
*/
ItemType Top() const;
/*
* Function: Returns value of the top item from the stack
* Precondition: Stack has been initialized
* Postcondition: If (stack is empty), exception EmptyStack is thrown,
* else value of the top item is returned
*/
~StackType(void);
/*
* Function: destructor
* Precondition: Stack has been initailized
* Postcondition: deallocate memory
*/
private:
int maxStack;
ItemType* item;
int top;
};
StackType.cpp:
#include "StackType.h"
#include <iostream>
#include <string>
using namespace std;
template<class ItemType>
StackType<ItemType>::StackType(int max)
{
maxStack = max;
top = -1;
item = new ItemType[maxStack];
}
template<class ItemType>
bool StackType<ItemType>::IsEmpty() const
{
return (top == -1);
}
template<class ItemType>
bool StackType<ItemType>::IsFull() const
{
return (top == maxStack - 1);
}
template<class ItemType>
void StackType<ItemType>::Push(ItemType newItem)
{
if(IsFull())
throw FullStack();
top++;
item[top] = newItem;
}
template<class ItemType>
void StackType<ItemType>::Pop()
{
if(IsEmpty())
throw EmptyStack();
top--;
}
template<class ItemType>
ItemType StackType<ItemType>::Top() const
{
if(IsEmpty())
throw EmptyStack();
return item[top];
}
template<class ItemType>
StackType<ItemType>::~StackType()
{
delete []item;
}
Thanks everyone in advanced :)
Update: Looks like the class is built and all is fine. But when I build a client code to test it, I get these errors:
1>client_code.obj : error LNK2019: unresolved external symbol "public: __thiscall StackType::~StackType(void)" (??1?$StackType@H@@QAE@XZ) referenced in function _main
1>client_code.obj : error LNK2019: unresolved external symbol "public: void __thiscall StackType::Push(int)" (?Push@?$StackType@H@@QAEXH@Z) referenced in function _main
1>client_code.obj : error LNK2019: unresolved external symbol "public: __thiscall StackType::StackType(int)" (??0?$StackType@H@@QAE@H@Z) referenced in function _main
1>C:\Users\Alakazaam\Desktop\Stack\Debug\Stack.exe : fatal error LNK1120: 3 unresolved externals
main.cpp
#include <iostream>
#include <string>
#include "StackType.h"
using namespace std;
int main()
{
int num;
StackType<int> stack(4);
for(int i = 0; i < 4; i++)
{
cin >> num;
stack.Push(num);
}
return 0;
}
Update:
I got the solution, that StackType.h and StackType.cpp have to be in the same header file StackType.h (StackType.cpp is not needed bc Im using Template. So whatever supposed to be in the StackType.cpp, just go to the bottom of StackType.h)
Thanks everyone for helping :)
template <class ItemType>
at the top of each class? Also: Indent you functions! – flight