【问题标题】:I need help with building a c++ stack template.. "StackType<int>::~StackType<int>(void)" error?我在构建 C++ 堆栈模板方面需要帮助。“StackType<int>::~StackType<int>(void)”错误?
【发布时间】:2011-07-28 11:20:57
【问题描述】:

我是编程新手,在我们的 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: 函数 _main 中引用的无法解析的外部符号“public: __thiscall StackType::~StackType(void)”(??1?$StackType@H@@QAE@XZ)

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

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

1>C:\Users\Alakazaam\Desktop\Stack\Debug\Stack.exe : 致命错误 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;
}

更新:

我得到了解决方案,StackType.h 和 StackType.cpp 必须在同一个头文件 StackType.h 中(我使用模板时不需要 StackType.cpp。所以无论 StackType.cpp 中应该包含什么,只需转到 StackType.h 的底部)

感谢大家的帮助:)

【问题讨论】:

  • 请不要说“它不起作用”。说出发生了什么,以及你期望会发生什么。你得到的“很多错误”是什么?
  • 错误具体在哪里?根植于您的代码的负担不在于我们。
  • 呃……你不用把template &lt;class ItemType&gt;放在每个类的顶部吗?另外:缩进你的函数!
  • 好吧,我们应该用模板构建一个 c++ 类堆栈类型,仅此而已.. 我认为这样做的目标是构建它没有任何错误,以便我们以后可以使用它..跨度>
  • @quasiverse:您的意思是在 StackType.h 中的每个类函数之上?

标签: c++ class templates stack


【解决方案1】:

改变这个:

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对象并不难

【讨论】:

  • 谢谢马丁,你帮了大忙。关于 .tpp,我认为我的教授不会在这堂课上允许它
  • 我在尝试通过构建客户端代码测试类时遇到错误,请帮助.. 谢谢 :)
【解决方案2】:

您应该将此代码放在http://codereview.stackexchange.com 上。

【讨论】:

    猜你喜欢
    • 2012-03-18
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 2013-12-13
    • 2016-02-11
    相关资源
    最近更新 更多