These are the errors I am getting. I have never worked with classes before, I posted before but have done some work to it since then, and have gotten less errors. The main problems are the push function, the copy constructor, and the function that creates a new stack.
prog4.cpp: In function `int main()':
prog4.cpp:49: warning: passing `float' for converting 1 of `void StackType<ItemType>:: Push(ItemType) [with ItemType = int]'
Stack.cpp:21: error: expected constructor, destructor, or type conversion before '&' token
Stack.cpp:42: error: expected constructor, destructor, or type conversion before '(' token
#ifndef STACK_H
#define STACK_H
const int MAX_SIZE = 10;
class FullStack
{};
class EmptyStack
{};
template <class ItemType>
class StackType
{
public:
StackType (int size = MAX_SIZE);
~StackType();
StackType(const StackType &right);
void Clear ();
bool IsFull () const;
bool IsEmpty () const;
void Push (ItemType item);
ItemType Pop ();
ItemType Top ();
void Print ();
const StackType<ItemType>& operator= (const StackType<ItemType>&);
private:
ItemType *items;
int stackSize;
int top;
};
#endif
Stack.cpp
#include "Stack.h"
#include <iostream>
using namespace std;
template <class ItemType>
StackType<ItemType>::StackType(int size)
{
top = -1;
stackSize = size;
items = new int[stackSize];
}
template <class ItemType>
StackType<ItemType>::~StackType ()
{
delete [] items;
}
template <class ItemType>
StackType& StackType<ItemType>::operator = (const StackType &right)
{
if (&right != this)
{
if (stackSize != right.stackSize)
{
delete [] items;
stackSize = right.stackSize;
items = new ItemType[stackSize];
}
top = right.top;
for (int x = 0; x < stackSize; x++)
{
items[x] = right.items[x];
}
}
return *this;
}
template <class ItemType>
StackType<ItemType>::StackType (&right)
{
stackSize = right.stackSize;
top = right.top;
items = new int [stackSize];
for (int x = 0; x < stackSize; x++)
{
items[x] = right.items[x];
}
}
template <class ItemType>
void StackType<ItemType>::Clear()
{
top = -1;
}
template <class ItemType>
bool StackType<ItemType>::IsEmpty() const
{
return (top == -1);
}
template <class ItemType>
bool StackType<ItemType>::IsFull() const
{
return (top == stackSize-1);
}
template <class ItemType>
void StackType<ItemType>::Push (ItemType item)
{
if (IsFull())
{
throw FullStack();
}
else
{
top++;
items[top] = item;
}
}
template <class ItemType>
ItemType StackType<ItemType>::Pop()
{
if (IsEmpty())
{
throw EmptyStack();
}
else
{
top--;
}
}
template <class ItemType>
ItemType StackType<ItemType>:: Top()
{
if (IsEmpty())
{
throw EmptyStack();
}
return items[top];
}
template <class ItemType>
void StackType<ItemType>::Print()
{
int count;
count = stackSize;
if (stackSize >= -1)
{
cout << "This stack contains: ";
while (count >= -1)
{
cout << items[count]<< " ";
}
cout << endl;
}
else
{
throw EmptyStack();
}
}
This is the Client Code - where I think most of the problems are occurring
#include <iostream>
#include "Stack.h"
using namespace std;
int main()
{
StackType<int> stack;
float item;
int num;
int size;
cout << "How many items would you like in this stack (1 - 10): ";
cin >> size;
if (size > 0 && size <= 10)
size = size;
else
{
cout << endl << "Not a valid number, using default of 10";
size = MAX_SIZE;
}
while (num != 0)
{
cout << "Please select from the following options:" << endl;
cout << '\t' << "1 - Push an item" << endl;
cout << '\t' << "2 - Pop an item" << endl;
cout << '\t' << "3 - Get the top item" << endl;
cout << '\t' << "4 - Determine if stack is full" << endl;
cout << '\t' << "5 - Determine if stack is empty" << endl;
cout << '\t' << "6 - Clear all items from stack" << endl;
cout << '\t' << "7 - Print the stack" << endl;
cout << '\t' << "8 - Copy stack" << endl;
cout << '\t' << "9 - Assign one stack to another" << endl;
cout << '\t' << "0 - Quit program" << endl;
cout << endl;
cout << '\t' << "Selection: ";
cin >> num;
cout << endl;
if (num == 1)
{
cout << "Enter an integer to push into the stack: ";
cin >> item;
try
{
stack.Push(item);
cout << endl << item << " successfully pushed" << endl;
}
catch (FullStack exceptionObject)
{
cerr << endl << "Stack is full - unable to push item onto stack" << endl;
}
}
else if (num == 2)
{
try
{
cout << endl << stack.Pop() << " successfully popped" << endl;
}
catch (EmptyStack exceptionObject)
{
cerr << endl << "Stack is empty - unable to pop an item" << endl;
}
}
else if (num == 3)
{
try
{
cout << endl << stack.Top() << " is currently on top of the stack" << endl;
}
catch (EmptyStack exceptionObject)
{
cout << endl;
cerr << "Cannot retrieve integer from top of list - list is currently empty" << endl;
}
}
else if (num == 4)
{
if (stack.IsFull() == true)
{
cout << endl << "The stack is currently full" << endl;
}
else
{
cout << endl << "The stack is NOT currently full" << endl;
}
}
else if (num == 5)
{
if (stack.IsEmpty () == true)
{
cout << endl << "The stack is currently empty" << endl;
}
else
{
cout << endl << "The stack is NOT currently empty" << endl;
}
}
else if (num == 6)
{
stack.Clear();
cout << endl << "The stack was successfully cleared" << endl;
}
else if (num == 7)
{
try
{
cout << endl;
stack.Print ();
cout << endl;
}
catch (EmptyStack exceptionObject)
{
cerr << endl << "There are no items in this stack" << endl;
}
}
else if (num == 8)
{
cout << endl << "Old Stack: ";
stack.Print();
cout << endl << "New Stack: ";
stack.Print();
cout << endl;
}
else if (num == 9)
{
cout << endl << "Old Stack: ";
stack.Print();
cout << endl << "New Stack: ";
stack.Print();
cout << endl;
}
else if (num < 0 || num > 9)
{
cout << endl << "Invalid input, please try again" << endl;
}
}
return 0;
}
Like I said, I have never worked with class templates or stacks before, so I dont really know what I'm doing. Any help, suggestions, criticism would be gladly accepted... Thanks