c – 用于类封装的模板参数的默认参数

码:
template <typename element_type,typename container_type = std::deque<element_type> >
class stack
{
    public:
        stack() {}
        template <typename CT>
        stack(CT temp) : container(temp.begin(),temp.end()) {}
        bool empty();
   private:
       container_type container;
};

template <typename element_type,typename container_type = std::deque<element_type> >
bool stack<element_type,container_type>::empty()
{
    return container.empty();
}

当我编译它给出错误.

default argument for template parameter for class enclosing 'bool stack<element_type,container_type>::empty()'

为什么编译器抱怨,我该怎么办?

解决方法

您尝试为第二个模板参数提供一个认参数,以堆栈两次.认模板参数,就像函数参数一样,只能定义一次(每个翻译单元);甚至不能重复完全相同的定义是允许的.

只需在定义类模板的开头键入认参数即可.之后,请注意:

template<typename element_type,typename container_type>
bool stack<element_type,container_type>::empty(){
    return container.empty();
}

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...