预期在“sktraits”之前有嵌套名称说明符
这是导致编译错误的类模板的片段:
/* Secondary index class */
template<class TKey, class TVal, class key_traits, class val_traits>
template<class TSecKey, class sktraits>
class CBtreeDb<TKey, TVal, key_traits, val_traits>::CDbSecondaryIndex: protected CBtreeDb<TKey, TVal>, public IDeallocateKey
{
public:
typedef TSecKey skey_type;
typedef typename sktraits skey_traits;
typedef CNewDbt<TSecKey, sktraits> CDbSKey;
typedef typename iterator_t<TSecKey, skey_traits> iterator;
typedef typename iter_lower_bound_t<skey_type> iter_lower_bound;
typedef typename iter_upper_bound_t<skey_type> iter_upper_bound;
CDbSecondaryIndex(CDbEnv* pEnv, u_int32_t flags, bool bAllowDuplicates=false):
CBtreeDb(pEnv, flags, bAllowDuplicates)
{
}
// Class implementation continues ...
};
我得到的编译器错误消息是:
expected nested-name-specifier before 'sktraits'.
实际上,此错误发生在每个 typedef
声明上,后跟 typename
我有过去在 XP 上使用 VS2005 和 VS2008 成功编译了此代码。
我目前正在 Ubuntu 9.10 上构建,使用 gcc 4.4.1
我在 Google 上查找了此错误,看来 typename
在线上(发生错误的地方)不是必需的,因为标准假设该位置的标识符是一种类型。 g++ 似乎在抱怨,因为它期望任何 typename
声明都是合格的(即 A::B)。
这是对问题的正确诊断吗?如果是,那么我如何“完全限定”typename
?
简而言之,我该如何解决这个问题?
This is a snippet of a class template which is causing compilation errors:
/* Secondary index class */
template<class TKey, class TVal, class key_traits, class val_traits>
template<class TSecKey, class sktraits>
class CBtreeDb<TKey, TVal, key_traits, val_traits>::CDbSecondaryIndex: protected CBtreeDb<TKey, TVal>, public IDeallocateKey
{
public:
typedef TSecKey skey_type;
typedef typename sktraits skey_traits;
typedef CNewDbt<TSecKey, sktraits> CDbSKey;
typedef typename iterator_t<TSecKey, skey_traits> iterator;
typedef typename iter_lower_bound_t<skey_type> iter_lower_bound;
typedef typename iter_upper_bound_t<skey_type> iter_upper_bound;
CDbSecondaryIndex(CDbEnv* pEnv, u_int32_t flags, bool bAllowDuplicates=false):
CBtreeDb(pEnv, flags, bAllowDuplicates)
{
}
// Class implementation continues ...
};
The compiler error message I get is:
expected nested-name-specifier before 'sktraits'.
Actually, this error occurs on every typedef
declaration followed by typename
I have compiled this code succesfully in the past using VS2005 and VS2008 on XP.
I am currently building on Ubuntu 9.10, using gcc 4.4.1
I looked this error up on Google and it appears that the typename
isn't necessary on the line (where the error occurs), because the standard assumption is that an identifier in that position is a type. g++ seems to be complaining because it expects any typename
declaration there to be qualified (i.e. A::B).
Is this is a correct diagnosis of the problem - if so, then how do I "fully qualify" the typename
?
In short, how may I resolve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
需要
typename
来指定依赖名称实际上是一种类型。您的名称不是从属名称,因此不需要或不允许使用typename
。更新 标准实际上有这样的语法定义:
另外两个可以使用 typename 关键字的地方是模板参数列表和 using 声明(在后一种情况下,它也必须跟有嵌套名称说明符)。
typename
is needed to specify that a dependent name is in fact a type. Your names are not dependent names, so notypename
is required or allowed.Update The standard actually has this syntax definition:
The two other places you can use the typename keyword are in the template parameter list and in the
using
declaration (in the latter case it too must be followed by a nested name specifier).不允许以下情况:
在类/函数定义之前最多可以有一个
template
规范。The following is not allowed:
You can have at most one
template<>
specification before a class/function definition.