error: default argument for template parameter for class enclosing

May 15, 2020 at 12:13pm
I am trying to implement a std::list-like doubly linked list and when defining a constructor I am coming across an error that I don't quite understand.

The stripped down code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

template <typename T>
class List
{
public: 
   template<typename InputIt, typename = std::_RequireInputIter<InputIt>>
   List(InputIt first, InputIt last);

};

template<typename T>
template<typename InputIt, typename = std::_RequireInputIter<InputIt>>
List<T>::List(InputIt first, InputIt last)
{
   // some code here
}


int main()
{

}


When trying to compile an error is thrown:

error: default argument for template parameter for class enclosing 'List<T>::List(InputIt, InputIt)'|

The error message is not clear for me, anyone to shine a light?
Thanks!!
Last edited on May 15, 2020 at 12:19pm
May 15, 2020 at 12:18pm
template<typename T>
template<typename InputIt, typename = std::_RequireInputIter<InputIt>>
List<T>::List(I....


Why do you have two template things stacked on top of each other?
May 15, 2020 at 2:47pm
@highwayman

template<typename T> is for List<T>

template<typename InputI... is for List(InputIt first, InputIt last)
May 15, 2020 at 2:48pm
Ahhh wait I get it now ok cool.
May 15, 2020 at 2:59pm
Default arguments only belong in the declaration:
1
2
3
4
5
6
template<typename T>
template<typename InputIt, typename>
List<T>::List(InputIt first, InputIt last)
{
   // some code here
}
Last edited on May 15, 2020 at 3:35pm
May 15, 2020 at 3:14pm
@mbozzi oh I see, Thanks

Is there any workaround for that?
Last edited on May 15, 2020 at 3:15pm
May 15, 2020 at 3:34pm
Yes, if it wasn't clear from the above, here's a version that compiles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

template <typename T>
class List
{
public: 
   template<typename InputIt, typename = std::_RequireInputIter<InputIt>>
   List(InputIt first, InputIt last);

};

template<typename T>
template<typename InputIt, typename>
List<T>::List(InputIt first, InputIt last)
{
   // some code here
}

int main()
{

} 
Topic archived. No new replies allowed.