Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default argument for template parameter for class enclosing

Tags:

code:

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(); } 

When I compile it gives the error.

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

Why is the compiler complaining and how can I make it work?

like image 702
huals Avatar asked Jun 12 '13 16:06

huals


1 Answers

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(); } 
like image 96
aschepler Avatar answered Sep 20 '22 16:09

aschepler