default argument for template parameter for class enclosing

后端 未结 2 1296
终归单人心
终归单人心 2021-02-12 11:21

code:

template  >
class stack
{
    public:
        stack() {}
        temp         


        
相关标签:
2条回答
  • 2021-02-12 11:27

    That default parameter is, by syntax, a default parameter to the class, and it only makes sense at the class declaration.

    If you would call that function ...

    stack<foo,bar>().empty();
    

    you only have the template parameters on the site of the class name, for which you already provided default parameters at the point of the template class declaration.

    You can solve the issue by simply removing the default parameter from the function definition:

    template<typename element_type,typename container_type>
    bool stack<element_type,container_type>::empty(){
        return container.empty();
    }
    
    0 讨论(0)
  • 2021-02-12 11:50

    You attempt to provide a default argument for the second template parameter to stack twice. Default template arguments, just like default function arguments, may only be defined once (per translation unit); not even repeating the exact same definition is allowed.

    Just type the default argument at the beginning where you define the class template. After that, leave it out:

    template<typename element_type,typename container_type>
    bool stack<element_type,container_type>::empty(){
        return container.empty();
    }
    
    0 讨论(0)
提交回复
热议问题