Curtis Phillips
Curtis Phillips

Reputation: 1

C++ Template Variadic Class Constructor working with Parameter Pack but functions not

So the problem I am facing is that my Data Structure class constructor works allowing me to declare the class with variable length of parameters:

template<class T>
class Dynarray
{
    private:
        int size;
    public:
        template<class T, typename... Arguments>
        Dynarray(T item,Arguments...)
        {
            size = sizeof...(Arguments);
        }
}

However if I add the additional public member function so I can add more to the class like so:

template<class T>
class Dynarray
{
    private:
        int size;
    public:
        template<class T, typename... Arguments>
        Dynarray(T item,Arguments...)
        {
            size = sizeof...(Arguments);
        }
        /////////////////////////////////////////////////////////
        template<class T, typename... Arguments>
        void Dynarray<T>::AddGroup(T item, Arguments...)
        {   //Errors C2838, C2059, C2334

            size += sizeof...(Arguments);

        }
        /////////////////////////////////////////////////////////
}

I get error codes:

C2838 'AddGroup': illegal qualified name in member declaration

C2059 syntax error: '{'

C2334 unexpected token(s) preceding '{'; skipping apparent function body

Is there a difference when it comes class templates between Constructors and Member Functions like this? Do you know of any workarounds?

Upvotes: 0

Views: 894

Answers (1)

Jarod42
Jarod42

Reputation: 217448

Inside the class definition, you should not repeat Dynarray<T>: so it should be:

template<typename... Arguments>
void AddGroup(T item, Arguments...)
{
    size += sizeof...(Arguments);
}

(I also remove the duplicate typename T which is already present for the class.)

Upvotes: 3

Related Questions