为什么我在使用模板时不能使用前向声明?

Ada*_*Lee 1 c++ templates

对于非模板类,我可以在头文件中使用前向声明.但是当我在模板类中使用它时,它给了我错误.

我必须包含头文件,我想知道原因.

class Pu;

template <typename T>
class Pt()
{
     void test(Pu<T> u);
}
Run Code Online (Sandbox Code Playgroud)

Lig*_*ica 6

它工作正常,如果你做得好!

请记住,Pu是一个类模板,而不是一个类,所以它必须声明为:

template <typename T>      // N.B. this line
class Pu;

template <typename T>
class Pt                   // N.B. no "()"
{
     void test(Pu<T> u);
};                         // N.B. ";"
Run Code Online (Sandbox Code Playgroud)

(现场演示)