Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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++ 为什么我需要声明下溢错误_C++ - Fatal编程技术网

C++ 为什么我需要声明下溢错误

C++ 为什么我需要声明下溢错误,c++,C++,在下面的代码中,我得到了一个错误 Stack.cpp:在成员函数“T*Stack::pop()”中: Stack.cpp:53:error:没有依赖于模板参数的'underflow\u error'参数,因此必须提供'underflow\u error'声明 声明类下溢错误背后的基本原理是什么 #include <iostream> using namespace std; template <class T> class Stack { public: Sta

在下面的代码中,我得到了一个错误

Stack.cpp:在成员函数“T*Stack::pop()”中:
Stack.cpp:53:error:没有依赖于模板参数的'underflow\u error'参数,因此必须提供'underflow\u error'声明

声明
类下溢错误背后的基本原理是什么

#include <iostream>
using namespace std;

template <class T>
class Stack
{
public:
    Stack(): head(NULL) {};
    ~Stack();

    void push(T *);
    T* pop();

protected:
    class Element {
    public:
            Element(Element * next_, T * data_):next(next_), data(data_) {}
            Element * getNext() const { return next; }
            T * value() const {return data;}
    private:
            Element * next;
            T * data;
    };

    Element * head;
};

template <class T>
Stack<T>::~Stack()
{
    while(head)
    {
            Element * next = head->getNext();
            delete head;
            head = next;
      }
 }

template <class T>
T * Stack<T>::pop()
{
    Element *popElement = head;
    T * retData;

    if(head == NULL)
            throw underflow_error("stack is empty");

    retData = head->value();
    head = head->getNext();

    delete popElement;
    return retData;
}
#包括
使用名称空间std;
模板
类堆栈
{
公众:
Stack():head(NULL){};
~Stack();
无效推力(T*);
T*pop();
受保护的:
类元素{
公众:
元素(元素*下一个,T*数据):下一个(下一个),数据(数据){
元素*getNext()常量{return next;}
T*value()常量{返回数据;}
私人:
元素*下一个;
T*数据;
};
元素*头;
};
模板
堆栈::~Stack()
{
while(head)
{
元素*next=head->getNext();
删除标题;
头=下一个;
}
}
模板
T*Stack::pop()
{
元素*popElement=头部;
T*retData;
if(head==NULL)
抛出下溢错误(“堆栈为空”);
retData=head->value();
head=head->getNext();
删除pop元素;
返回数据;
}
您必须添加

#include <stdexcept>
#包括
使用
下溢\u error

时,必须添加

#include <stdexcept>
#包括

当您使用
下溢\u错误

奇数查看时,它并不是说在中存在下溢\u错误声明stdexcept@Jimm在广告图片的正上方,您将看到:Reference>stdexcept>underflow_errorOdd,看,它并没有说underflow_错误声明存在于stdexcept@Jimm就在广告图片的正上方,您将看到:Reference>stdexcept>underflow\u error您不应该在头文件中使用using指令(
using namespace X
),也不应该在头文件中使用using指令(
using namespace X
)。