我需要帮助来构建 c++堆栈模板..“StackType::~StackType(void)”错误?
我是编程新手,在我们的 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 : error LNK2019: unresolved external symbol "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
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)
感谢大家的帮助:)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改此:
应该是:
注意 1:使用
class ItemType
是正确的,但因为 ItemType 可能是非类类型,所以我更喜欢使用类型名形式:注意 2:由于模板的工作方式。通常最好将方法定义放在头文件中(与类一起)。它可以处理 cpp 文件,但需要额外的工作。最简单的解决方案是:
注3:
using namespace std;
是不好的做法(所有书籍都这样做是为了节省空间,从长远来看,你会发现最好不要这么做)。在 std 对象上添加std::
前缀并不困难Change this:
Should be:
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: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:
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 withstd::
您应该将此代码放在 http://codereview.stackexchange.com 上。
You should put this code on http://codereview.stackexchange.com .