简体   繁体   English

C ++模板可变参数类构造函数与参数包一起使用,但功能不起作用

[英]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 : 所以我面临的问题是我的数据结构类constructor可以使我声明具有可变长度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: 但是,如果我添加了额外的public成员函数,则可以像这样向类中添加更多:

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 C2838'AddGroup':成员声明中的非法合格名称

C2059 syntax error: '{' C2059语法错误:“ {”

C2334 unexpected token(s) preceding '{'; C2334'{'之前的意外令牌; skipping apparent function body 跳过表观功能体

Is there a difference when it comes class templates between Constructors and Member Functions like this? 这样的ConstructorsMember Functions之间的类模板是否有所不同? Do you know of any workarounds? 您知道任何解决方法吗?

Inside the class definition, you should not repeat Dynarray<T> : so it should be: 在类定义内,您不应重复Dynarray<T> :因此应为:

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.) (我还删除了该类已经存在的重复typename T

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM