【问题标题】:C++ How to use a class with template argument list when the template argument type(s) is still unknown?当模板参数类型仍然未知时,C++ 如何使用带有模板参数列表的类?
【发布时间】:2015-08-06 01:09:20
【问题描述】:

我有一个采用模板参数列表的方法定义。在该方法中,我需要使用另一个传入模板参数类型的泛型类作为方法的模板参数类型。如何做到这一点?

template <typename T>
void MyQueue<T>::push(T* object)
{
    Wrapper<T>* wr = new Wrapper(object); //error here
    if (*tail)
    {
        tail->next = wr;
    }
    else
    {
        head = wr;
    }
    tail = wr;
    wr->next = nullptr;
}

错误:

error C2955: 'Wrapper': use of class template requires template argument list

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    Wrapper 是一个类模板,所以在这种情况下你不能说new Wrapper(args)。您只需在正确的位置添加模板参数即可:

    Wrapper<T>* wr = new Wrapper<T>(object);
                                ^^^
    

    提示在 LHS 上的指针类型中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多