在编程的新手方面,在我们的C ++课程中,我们必须构建一个堆栈类并使用模板,我遵循了本书并尝试将其放在一起,但仍然存在很多错误。因此,我希望这里的专家能够帮助我指出正确的方向。

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;
}


谢谢大家先进:)

更新:
看起来该类已建立并且一切都很好。但是,当我构建一个客户端代码对其进行测试时,会出现以下错误:

1> client_code.obj:错误LNK2019:未解析的外部符号“ public:__thiscall StackType ::〜StackType(void)”(?? 1?$ StackType @ H @@ QAE @ XZ)在函数_main中引用

1> client_code.obj:错误LNK2019:未解析的外部符号“ public:void __thiscall StackType :: Push(int)”(?Push @?$ StackType @ H @@ QAEXH @ Z)在函数_main中引用

1> client_code.obj:错误LNK2019:未解析的外部符号“ public:__thiscall StackType :: StackType(int)”(?? 0?$ StackType @ H @@ QAE @ H @ Z)在函数_main中引用

1> C:\ Users \ Alakazaam \ Desktop \ Stack \ Debug \ Stack.exe:致命错误LNK1120:3个未解决的外部组件

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;
}


更新:

我得到了解决方案,即StackType.h和StackType.cpp必须在同一头文件StackType.h中(使用模板使用Im不需要StackType.cpp。因此,应该放在StackType.cpp中的任何内容,只需转到StackType.h的底部)

谢谢大家的帮助:)

最佳答案

更改此:

template <class ItemType>

class FullStack
{};

class EmptyStack
{};

class StackType


应该:

class FullStack
{};

class EmptyStack
{};

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


注意1:class ItemType的使用是正确的,但是因为ItemType可能是非类类型,所以我更喜欢使用typename形式:

template<typename ItemType>
class StackType


注意2:由于模板的工作方式。通常,最好将方法定义放在头文件中(与类一起)。它可以处理cpp文件,但需要额外的工作。您的简易列表解决方案是:


将“ StackType.cpp”重命名为“ StackType.tpp”
将“ #include ”添加到“ StackType.h”的末尾


注意3:using namespace std;是一种不好的做法(从长远来看,所有书籍都这样做以节省空间,您会发现它不太好)。给std对象加上std::前缀并不困难

09-06 19:31