6

这是导致编译错误的类模板的片段:

/* 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 成功编译了此代码。

我目前正在使用 gcc 4.4.1 在 Ubuntu 9.10 上构建

我在谷歌上查看了这个错误,似乎typename不需要在线(发生错误的地方),因为标准假设是该位置的标识符是一种类型。g++ 似乎在抱怨,因为它希望typename那里的任何声明都是合格的(即 A::B)。

这是对问题的正确诊断吗?如果是,那我该如何“完全符合”typename呢?

简而言之,我该如何解决这个问题?

4

2 回答 2

7

typename需要指定从属名称实际上是一种类型。您的名称不是从属名称,因此 notypename是必需的或允许的。

更新标准实际上有这样的语法定义:

typename-specifier :
    typename nested-name-specifier 标识符
    typename nested-name-specifier template opt simple-template-id

可以使用 typename 关键字的另外两个地方是在模板参数列表和using声明中(在后一种情况下,它也必须跟在一个嵌套的名称说明符之后)。

于 2011-12-01T04:38:23.497 回答
1

以下是不允许的:

template<class A>
template<class B> class F { ... };

template<>在类/函数定义之前最多可以有一个规范。

于 2010-02-17T12:37:17.473 回答