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-&gt;cell = first;
        list-&gt;next = NULL;
        head = list;
        Init(arg ...);
    }
    
    void Init(T now, Args ... arg)
    {
        element *list = new element();
        list-&gt;cell = now;
        element* next = head;
        while (!next.next)
            next = next-&gt;next;
    
        next = list;
        list.next = NULL;
        Init(arg ...); // вот здесь C2661
    }
    
     void Init(T now)
    {
        element *list = new element();
        list-&gt;cell = now;
        element* next = head;
        while (!next-&gt;next) {
            next = next-&gt;next;
        }
        next = list;
        list-&gt;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-&gt;cell = first;
        list-&gt;next = NULL;
        head = list;
        Init(arg ...);
    }
    
    template &lt;typename TInner, typename ... ArgsInner&gt;
    void Init(TInner now, ArgsInner ... arg)
    {
        element *list = new element();
        list-&gt;cell = now;
        element* next = head;
        while (!next-&gt;next)
            next = next-&gt;next;
    
        next = list;
        list-&gt;next = NULL;
        Init(arg ...);
    }
    
    void Init(T now)
    {
        element *list = new element();
        list-&gt;cell = now;
        element* next = head;
        while (!next-&gt;next) {
            next = next-&gt;next;
        }
        next = list;
        list-&gt;next = NULL;
    }
    

    };

    int main()
    {
    MyVec<int, int, int> p2(4, 5, 6);
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2