【发布时间】: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 <class ItemType>放在每个类的顶部吗?另外:缩进你的函数! -
好吧,我们应该用模板构建一个 c++ 类堆栈类型,仅此而已.. 我认为这样做的目标是构建它没有任何错误,以便我们以后可以使用它..跨度>
-
@quasiverse:您的意思是在 StackType.h 中的每个类函数之上?