为什么在使用模板时不能使用前向声明?
why I cannot use forward declaration when using template?
对于非模板class,我可以在头文件中使用前向声明。但是当我在模板 class.
中使用它时它给了我错误
我必须包含头文件,我想知道原因。
class Pu;
template <typename T>
class Pt()
{
void test(Pu<T> u);
}
如果操作得当,效果很好!
请记住,Pu
是一个 class 模板 ,而不是 class,因此必须这样声明:
template <typename T> // N.B. this line
class Pu;
template <typename T>
class Pt // N.B. no "()"
{
void test(Pu<T> u);
}; // N.B. ";"
(live demo)
对于非模板class,我可以在头文件中使用前向声明。但是当我在模板 class.
中使用它时它给了我错误我必须包含头文件,我想知道原因。
class Pu;
template <typename T>
class Pt()
{
void test(Pu<T> u);
}
如果操作得当,效果很好!
请记住,Pu
是一个 class 模板 ,而不是 class,因此必须这样声明:
template <typename T> // N.B. this line
class Pu;
template <typename T>
class Pt // N.B. no "()"
{
void test(Pu<T> u);
}; // N.B. ";"