标识符语法错误 'iterator'
Syntax error for identifier 'iterator'
以下代码在 VS 2013 中抛出两个编译器错误:
- 模板函数定义抛出
error C2061: syntax error : identifier 'iterator
'
- 该模板函数的特化抛出
error C2912: explicit specialization 'double getFillIn<double,double>(fillInOptions,double,std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,inputLoader *,va_list)
' is not a specialization of a function template
谁能解释一下为什么?我相当确定第二个错误只是第一个错误的结果,但我不明白为什么它无法弄清楚 iterator
标识符。
#include <map>
template <typename T> class table {
};
template <typename S, typename T>
void f(S s, std::map<S, table<T>*>::iterator it);
因为编译器还不知道 std::map<S, table<T>*>
的类型,所以它还不知道 std::map<S, table<T>*>::iterator
是一个类型(它可能是一个成员)。您需要使用 typename
关键字告诉它它将是一个类型:
template <typename S, typename T>
void f(S s, typename std::map<S, table<T>*>::iterator it);
// ^^^^^^^^
以下代码在 VS 2013 中抛出两个编译器错误:
- 模板函数定义抛出
error C2061: syntax error : identifier '
iterator
' - 该模板函数的特化抛出
error C2912: explicit specialization '
double getFillIn<double,double>(fillInOptions,double,std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,inputLoader *,va_list)
' is not a specialization of a function template
谁能解释一下为什么?我相当确定第二个错误只是第一个错误的结果,但我不明白为什么它无法弄清楚 iterator
标识符。
#include <map>
template <typename T> class table {
};
template <typename S, typename T>
void f(S s, std::map<S, table<T>*>::iterator it);
因为编译器还不知道 std::map<S, table<T>*>
的类型,所以它还不知道 std::map<S, table<T>*>::iterator
是一个类型(它可能是一个成员)。您需要使用 typename
关键字告诉它它将是一个类型:
template <typename S, typename T>
void f(S s, typename std::map<S, table<T>*>::iterator it);
// ^^^^^^^^