加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 05:22:39 所属栏目:百科 来源:网络整理
导读:码: template typename element_type,typename container_type = std::dequeelement_type class stack{ public: stack() {} template typename CT stack(CT temp) : container(temp.begin(),temp.end()) {} bool empty(); private: container_type containe
码:
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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读