Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我需要帮助建立一个c++;堆栈模板&引用;StackType<;int>;::~StackType<;int>;(无效)“;错误? 我对编程很感兴趣,在C++课程中,我们必须建立一个栈类和使用模板,我跟着这本书,试图把它放在一起,但仍然有很多错误。所以我希望这里的专家能帮我指出正确的方向_C++_Class_Templates_Stack - Fatal编程技术网

我需要帮助建立一个c++;堆栈模板&引用;StackType<;int>;::~StackType<;int>;(无效)“;错误? 我对编程很感兴趣,在C++课程中,我们必须建立一个栈类和使用模板,我跟着这本书,试图把它放在一起,但仍然有很多错误。所以我希望这里的专家能帮我指出正确的方向

我需要帮助建立一个c++;堆栈模板&引用;StackType<;int>;::~StackType<;int>;(无效)“;错误? 我对编程很感兴趣,在C++课程中,我们必须建立一个栈类和使用模板,我跟着这本书,试图把它放在一起,但仍然有很多错误。所以我希望这里的专家能帮我指出正确的方向,c++,class,templates,stack,C++,Class,Templates,Stack,堆栈类型.h: class FullStack {}; class EmptyStack {}; template<class ItemType> class StackType { public: StackType(int max); /* * Function: constructor * Precondition: none * Postcondition: Stack has been initialized

堆栈类型.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;
};
#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-FullStack
{};
类空堆栈
{};
模板
类堆栈类型
{
公众:
堆栈类型(int max);
/*
*函数:构造函数
*前提条件:无
*后置条件:堆栈已初始化
*/
bool IsEmpty()常量;
/*
*函数:确定堆栈是否为空
*前提条件:堆栈已初始化
*后置条件:函数值=(堆栈为空)
*/
bool IsFull()常量;
/*
*函数:确定堆栈是否已满
*前提条件:堆栈已初始化
*后置条件:函数值=(堆栈已满)
*/
无效推送(项目类型项目);
/*
*功能:将新项目添加到堆栈顶部
*前提条件:堆栈已初始化
*后置条件:如果(堆栈已满),则抛出异常FullStack,
*否则,新项位于堆栈顶部
*/
void Pop();
/*
*功能:从堆栈中删除顶部项
*前提条件:堆栈已初始化
*后置条件:如果(堆栈为空),则抛出异常EmptyStack,
*else顶部项已从堆栈中移除
*/
ItemType Top()常量;
/*
*函数:返回堆栈顶部项的值
*前提条件:堆栈已初始化
*后置条件:如果(堆栈为空),则抛出异常EmptyStack,
*返回顶部项的else值
*/
~StackType(void);
/*
*函数:析构函数
*前提条件:堆栈已初始化
*后置条件:释放内存
*/
私人:
int-maxStack;
项目类型*项目;
int top;
};

堆栈类型.cpp

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;
};
#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;
}
#包括“StackType.h”
#包括
#包括
使用名称空间std;
模板
StackType::StackType(int max)
{
maxStack=max;
top=-1;
item=新的ItemType[maxStack];
}
模板
bool StackType::IsEmpty()常量
{
返回(顶部==-1);
}
模板
bool StackType::IsFull()常量
{
返回(top==maxStack-1);
}
模板
void StackType::Push(ItemType newItem)
{
如果(IsFull())
抛出FullStack();
top++;
项目[顶部]=新项目;
}
模板
void StackType::Pop()
{
if(IsEmpty())
抛出空包();
顶部--;
}
模板
ItemType StackType::Top()常量
{
if(IsEmpty())
抛出空包();
返回项目[顶部];
}
模板
StackType::~StackType()
{
删除[]项;
}
感谢大家的先进:)

更新: 看起来这个班已经建好了,一切都很好。但是,当我构建一个客户端代码来测试它时,会出现以下错误:

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

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

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

1> C:\Users\alakazam\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”
使用名称空间std;
int main()
{
int-num;
堆垛式堆垛(4);
对于(int i=0;i<4;i++)
{
cin>>num;
stack.Push(num);
}
返回0;
}
更新:

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

感谢大家的帮助:)

更改此选项:

template <class ItemType>

class FullStack
{};

class EmptyStack
{};

class StackType
注2:由于模板的工作方式。通常最好将方法定义与类一起放在头文件中。它可以在cpp文件中工作,但需要额外的工作。最简单的解决方案是:

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

  • 注3:
    使用名称空间标准
    是一种不好的做法(所有的书都这样做是为了节省空间,从长远来看,你会发现它更好,但不是太好)。在std对象前面加上
    std::

    前缀并不困难。你应该把这个代码放在上面。

    请不要说“它不起作用”。说出发生了什么,以及你希望发生什么。你得到的“大量错误”是什么?具体错误在哪里?重担不在我们身上,我们需要彻底了解你的代码。。。你不需要把
    模板
    放在每个类的顶部吗?还有:缩进你的函数!我们应该用模板来构建一个C++类堆栈型,这就是它的原理。我认为这样做的目的是在没有任何错误的情况下构建它,以便我们以后可以使用它。@quasiverse:你的意思是在StackType.h中的每个类函数之上?谢谢Martin,你帮了很大的忙。关于.tpp,我认为我的教授不会允许它出现在这个类中,因为我在试图通过构建客户端代码来测试这个类时出错了,请帮助。。谢谢:)