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

StackType& StackType<ItemType>::operator = (const StackType &right) Needs to be: StackType<ItemType>& StackType<ItemType>::operator = (const StackType<ItemType> &right) The other error is the same issue.

StackType& StackType<ItemType>::operator = (const StackType &right) Needs to be: StackType<ItemType>& StackType<ItemType>::operator = (const StackType<ItemType> &right) The other error is the same issue.

StackType<ItemType>& StackType<ItemType>::StackType(const StackType<ItemType> &right)

when I put this in for the other function, it comes up with 2 errors
Stack.cpp:43: error: return type specification for constructor invalid
Stack.cpp:43: error: cannot declare reference to `void'

So I am more than likely not doing that correctly, how should this be implemented?

StackType<ItemType>::StackType (&right)

Should be:

StackType<ItemType>::StackType<ItemType> (const StackType<ItemType>& right)
StackType<ItemType>::StackType (&right)

Should be:

StackType<ItemType>::StackType<ItemType> (const StackType<ItemType>& right)

when I do that
Stack.cpp:43: error: invalid function declaration

I get this error

Updated code:

#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();
                StackType& operator=(const StackType &right);

        private:
                ItemType *items;
                int stackSize;
                int top;
};

#endif

Section that we are looking at on 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<ItemType>& StackType<ItemType>::operator = (const StackType<ItemType> &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<ItemType> (&right)
{
        stackSize = right.stackSize;
        top = right.top;
        items = new int [stackSize];

        for (int x = 0; x < stackSize; x++)
        {
                items[x] = right.items[x];
        }
}

SORRY for bugging you so much, I have never used class templates before and the book and notes make me even more confused than I was going into this project
Thanks for your help

Sorry, I edited the code. You must have copied it before I made the change. Look at my message again.

StackType(const StackType &right);

In your .h file should be:

StackType(const StackType<ItemType> &right);
StackType(const StackType &right);

In your .h file should be:

StackType(const StackType<ItemType> &right);

I now get these errors when I go to compile:

/tmp/ccXxm2AE.o(.text+0x123): In function `main':
: undefined reference to `StackType<int>::StackType(int)'
/tmp/ccXxm2AE.o(.text+0x401): In function `main':
: undefined reference to `StackType<int>::Push(int)'
/tmp/ccXxm2AE.o(.text+0x4ed): In function `main':
: undefined reference to `StackType<int>::Pop()'
/tmp/ccXxm2AE.o(.text+0x5c4): In function `main':
: undefined reference to `StackType<int>::Top()'
/tmp/ccXxm2AE.o(.text+0x68c): In function `main':
: undefined reference to `StackType<int>::IsFull() const'
/tmp/ccXxm2AE.o(.text+0x725): In function `main':
: undefined reference to `StackType<int>::IsEmpty() const'
/tmp/ccXxm2AE.o(.text+0x7ba): In function `main':
: undefined reference to `StackType<int>::Clear()'
/tmp/ccXxm2AE.o(.text+0x824): In function `main':
: undefined reference to `StackType<int>::Print()'
/tmp/ccXxm2AE.o(.text+0x8f2): In function `main':
: undefined reference to `StackType<int>::Print()'
/tmp/ccXxm2AE.o(.text+0x927): In function `main':
: undefined reference to `StackType<int>::Print()'
/tmp/ccXxm2AE.o(.text+0x980): In function `main':
: undefined reference to `StackType<int>::Print()'
/tmp/ccXxm2AE.o(.text+0x9b5): In function `main':
: undefined reference to `StackType<int>::Print()'
/tmp/ccXxm2AE.o(.text+0xa2b): In function `main':
: undefined reference to `StackType<int>::~StackType()'
/tmp/ccXxm2AE.o(.text+0xa49): In function `main':
: undefined reference to `StackType<int>::~StackType()'
collect2: ld returned 1 exit status

My .h file is now changed to

StackType(const StackType<ItemType> &right);

My .cpp file is now changed to

StackType<ItemType>::StackType<ItemType> (const StackType<ItemType>& right)

Post all of the code.

Stack.h

#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<ItemType> &right);
                void Clear();
                bool IsFull() const;
                bool IsEmpty() const;
                void Push(ItemType item);
                ItemType Pop();
                ItemType Top();
                void Print();
                StackType& operator=(const StackType<ItemType> &right);

        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<ItemType>& StackType<ItemType>::operator = (const StackType<ItemType> &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<ItemType> (const StackType<ItemType>& 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();
        }
}

If you see anything that will not work in the code, will you give me the heads up too? Thanks,
Ben

Stack.cpp:

StackType<ItemType>::StackType<ItemType>(const StackType<ItemType>& right)

Should be:

StackType<ItemType>::StackType(const StackType<ItemType>& right)

I think that was my fault.

In Stack.h:

StackType& operator=(const StackType<ItemType> &right);

Should be:

StackType<ItemType>& operator=(const StackType<ItemType> &right);

Still getting the same error messages

Copy everything in Stack.cpp into Stack.h (properly placed, of course). You are having some linking issue but I'm too tired to think properly. That should get you to compile it and test.

Thanks, that worked, now I just need to fix it so everything outputs as it should, will you please check back tomorrow in case I have any other questions about the program?

Thank You so much for your help
Ben

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.