c++ - 我需要有关构建 C++ 堆栈模板的帮助。 "StackType<int>::~StackType<int>(void)"错误?

标签 c++ class templates stack

我是编程新手,在我们的c++类(class)中,我们必须构建一个堆栈类并使用模板,我按照书本尝试将它们组合在一起但仍然有很多错误。所以我希望这里的专家能帮助我指明正确的方向。

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

先谢谢大家:)

更新: 看起来类(class)已经建立,一切都很好。但是当我构建一个客户端代码来测试它时,我得到了这些错误:

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 : fatal error LNK1120: 3 unresolved externals

主要.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 中(不需要 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 文件,但需要额外的工作。最简单的解决方案是:

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

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

并不难

关于c++ - 我需要有关构建 C++ 堆栈模板的帮助。 "StackType<int>::~StackType<int>(void)"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5526990/

相关文章:

.net - 字符串数组需要声明吗?

JavaScript:对类/函数和对象使用相同的名称?

c++ - 添加参数后“模板参数推导/替换失败”

c++ - 带模板的二进制搜索功能,用于数组-编译问题

c++ - C++余弦查找表

c++ - 标量类型的特征模板

c++ - 使用 Doxygen 在 C++ 中记录函数

c++ - 对 bool 函数指针的参数感到困惑

c++ - C++ 会使用复制赋值运算符中的 move 构造函数吗?

c++ - 如何删除 GDB 中的捕获点