0
votes

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 :)

2
Please, never say "It doesn't work." Say what happened, and what you expected to happen instead. What are the "lot of error" you are getting?dfan
Where are the errors specifically? The burden is not on us to root through your code.jonsca
Uhh... don't you have to put template <class ItemType> at the top of each class? Also: Indent you functions!flight
well, we supposed to build a c++ class stacktype with template and thats about it.. I think the goal of this is to build this without any error so we can use it later on..Kyle Mai
@quasiverse: you mean on top of each class function in StackType.h ?Kyle Mai

2 Answers

2
votes

Change this:

template <class ItemType>

class FullStack
{};

class EmptyStack
{};

class StackType

Should be:

class FullStack
{};

class EmptyStack
{};

template <class ItemType>
class StackType
// This tells the compiler StackType is a template that uses ItemType internally.

Note 1: The use of class ItemType is correct but because ItemType may be a non class type I prefer to use the typename form:

template<typename ItemType>
class StackType

Note 2: Because of the way templates work. It is usally best to put the method definition in the header file (along with the class). It can work it the cpp file but it takes extra work. The simplist solution for you is:

  1. Rename "StackType.cpp" to "StackType.tpp"
  2. Add "#include <StackType.tpp>" to the end of "StackType.h"

Note 3: using namespace std; is bad practice (All the books do it to save space in the long run you will find it better not too). It is not difficult to prefix std objects with std::

0
votes