Variadic template C2661
-
I decided to write my mini vector and something very strange.
Error C2661 'MyVec: Init': no overloaded function takes 6 arguments
Code itself:
template <typename T, typename ... Args> class MyVec { public: struct element { T cell; void* next; }; element* head;
MyVec(T first,Args ... arg) { element *list = new element(); list->cell = first; list->next = NULL; head = list; Init(arg ...); } void Init(T now, Args ... arg) { element *list = new element(); list->cell = now; element* next = head; while (!next.next) next = next->next; next = list; list.next = NULL; Init(arg ...); // вот здесь C2661 } void Init(T now) { element *list = new element(); list->cell = now; element* next = head; while (!next->next) { next = next->next; } next = list; list->next = NULL; }
};
void main()
{
MyVec<int,int,int,int,int,int,int> p2(4,5,3,5,4,3,4);
}
-
Problem is, you need to identify a whole series of functions.
Init
♪ That's what you're supposed to use.Got the code in a compilable species, but there's still time errors (NULL redesign), make it right.
#include <cstddef>
template <typename T, typename ... Args>
class MyVec
{
public:
struct element {
T cell;
element* next;
};
element* head;MyVec(T first, Args ... arg) { element *list = new element(); list->cell = first; list->next = NULL; head = list; Init(arg ...); } template <typename TInner, typename ... ArgsInner> void Init(TInner now, ArgsInner ... arg) { element *list = new element(); list->cell = now; element* next = head; while (!next->next) next = next->next; next = list; list->next = NULL; Init(arg ...); } void Init(T now) { element *list = new element(); list->cell = now; element* next = head; while (!next->next) { next = next->next; } next = list; list->next = NULL; }
};
int main()
{
MyVec<int, int, int> p2(4, 5, 6);
}