模板类对象作为模板类方法中的参数

查森

我认为解释我想要完成的事情的最好方法是通过代码:

template<typename T>
struct Foo {
   static void func(Bar<T> obj);
};              // ^ syntax error: identifier 'Bar'

template<typename T>
struct Bar {
   T data;
};
萨姆·瓦尔沙夫奇克

要解决这个问题,您只需要所谓的“前向声明”:

template<typename T>
struct Bar;

template<typename T>
struct Foo {
   static void func(Bar<T> obj); //syntax error no more
};

template<typename T>
struct Bar {
   T data;
};

Bar<T>首先引用where 时,编译器不知道它是什么。您只需要事先添加一个声明,模板的完整定义仍然可以在以后出现。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章