【问题标题】:why I cannot use forward declaration when using template? [duplicate]为什么我在使用模板时不能使用前向声明? [重复]
【发布时间】:2015-04-10 14:54:56
【问题描述】:

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

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

class Pu;

template <typename T>
class Pt()
{
     void test(Pu<T> u);
}

【问题讨论】:

  • 你能举个例子吗?
  • 我相信你正在寻找这个:stackoverflow.com/questions/495021/…
  • 前向声明的这种语法有效:template &lt;typename T&gt; class MyClass; - 你打算使用其他什么吗?
  • @Anatolyg,请将此移至答案,您是对的。

标签: c++ templates


【解决方案1】:

只要你操作得当,它就可以正常工作!

记住,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. ";"

(live demo)

【讨论】:

    猜你喜欢
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 2016-04-23
    • 2011-08-27
    相关资源
    最近更新 更多