gpt4 book ai didi

C++ 当模板参数类型仍然未知时,如何使用带有模板参数列表的类?

转载 作者:行者123 更新时间:2023-11-27 22:58:14 30 4
gpt4 key购买 nike

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

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

最佳答案

Wrapper 是一个类模板,因此您不能在此上下文中使用 new Wrapper(args)。您只需在正确的位置添加模板参数:

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

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

关于C++ 当模板参数类型仍然未知时,如何使用带有模板参数列表的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30423324/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com