【问题标题】:nested-name-specifier嵌套名称说明符
【发布时间】:2011-06-27 06:34:33
【问题描述】:

我有这样的代码:

namespace mymap {
    template <class Key,template <typename T > class Allocator> myownmap {
        typedef pair<const unsigned int, Key> typename _myPair;
        typedef multimap<unsigned int, Key,less<Key> ,Allocator<_myPair> > typename _entriesType;
    }
}

它在 MSVC 下编译成功(并且可以工作),但 gcc 抱怨语法无效:

.hpp:20: error: expected nested-name-specifier before ‘_myPair’
.hpp:20: error: two or more data types in declaration of ‘_myPair’

我做错了什么?

【问题讨论】:

  • 你真的用 gcc 测试过这段代码吗?您在 myownmap 之前缺少一个类/结构,并且“entriesPair”不会出现在您的 sn-p 中的任何位置。
  • 你能告诉我们entriesPair的定义吗?
  • 什么是myownmap?是函数还是类?

标签: c++ templates generic-programming


【解决方案1】:

那里不需要typename,因此是不允许的。

MSVC 在实际使用模板之前不会正确解析模板,因此直到以后才发现一些错误。

【讨论】:

    【解决方案2】:

    "expected nested-name-specifier" 表示在typename 关键字之后,您应该使用模板参数的一些嵌套名称,例如typedef typename Key::iterator ...。在您的情况下,您不必使用typename

    【讨论】:

      【解决方案3】:
      typedef pair<const unsigned int, Key> /*typename*/ _myPair;
                                            ^^^^^^^^^^^^ not needed
      

      请参阅gcc-4.5 output here。 (它适用于 myownmapclass 或函数)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多