gpt4 book ai didi

c++ - 指针生成的递归模板

转载 作者:搜寻专家 更新时间:2023-10-31 01:13:38 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

我想创建一个模板,它采用类型 T 和参数 N 作为参数,并为 T“给出”第 N 级的指针(例如,如果 T 是 int,N 是 2,它应该给出 int**)

到目前为止我的代码是:

template<class T,int N>
struct ptr
{
typedef ptr<T*,N-1>::t t;
};

template<class T>
struct ptr<T,0>
{
typedef T t;
};

int main()
{
ptr<int,3>::t a; //a should be int***
}

但它给了我这个编译器错误:

source.cpp:6:11: error: need 'typename' before 'ptr<T*, (N - 1)>::t' because 'ptr<T*, (N - 1)>' is a dependent scope

这是什么意思,如何解决(如果在 C++ 中可行)?

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