模板化函数语法错误中使用的模板化类中的嵌套类

Nested class in templated class used in templated function syntax error

本文关键字:嵌套 错误 函数 语法      更新时间:2023-10-16

假设我有以下内容:

template<typename T>
struct Foo
{
public:
class Bar
{           
};
};

如果我然后定义函数

template <typename T>
void func(Foo<T>::Bar g) { }

我收到一个错误: 语法错误:标识符"栏">

为什么我会收到错误以及如何解决它以便我可以使用模板化函数。

使用

template <typename T>
void func( typename Foo<T>::Bar g ) { }

否则,编译器不会将构造Foo<T>::Bar视为类型说明符,而是将其视为表达式。