首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么需要声明underflow_error

为什么需要声明underflow_error
EN

Stack Overflow用户
提问于 2013-05-21 10:23:04
回答 1查看 570关注 0票数 0

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

Stack.cpp: In member function ‘T* Stack<T>::pop()’: Stack.cpp:53: error: there are no arguments to ‘underflow_error’ that depend on a template parameter, so a declaration of ‘underflow_error’ must be available

声明class underflow_error;背后的理由是什么?

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-21 10:28:59

你必须添加

代码语言:javascript
复制
#include <stdexcept>

当您使用underflow_error时。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16661206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档