【问题标题】:Compilation error in specifying template arguments in boost::unordered_map [duplicate]在 boost::unordered_map 中指定模板参数时出现编译错误 [重复]
【发布时间】:2016-04-23 04:40:29
【问题描述】:

我正在为 Boost 无序地图容器编写包装器方法。在 boost Unordered Map 中有一个方法 begin(), 它返回一个指向第一个元素的迭代器。在我的包装器中,我试图编写一个模板化的包装器。例如

template< class Tkey, class Tvalue>
class CMyUnorderedMap
{
       boost::Unordered_map<TKey,  TValue> m_myMap;
    public:
       boost::unordered_map<TKey,  TValue>::iterator Begin();

};

template< class Tkey, class Tvalue> 
boost::unordered_map<TKey,  TValue>::iterator CMyUnorderedMap< TKey,  TValue >::Begin()
{
    return   m_myMap.begin()
}

在编译上述代码时(带有模板参数),我在 VS 2010 中遇到如下编译错误。

warningc4346: boost::unordered::unordered_map::iterator : 依赖名称不是类型。

error C3860 template argument list following class template name must list paramaters in the order used in tempate paramater list

但是,如果我在没有模板参数的情况下编译代码,则代码符合。 例如,如果像下面这样指定它可以工作

boost::unordered_map< std::string,  std::string>::iterator Begin();

有人帮忙吗

【问题讨论】:

    标签: c++ templates boost


    【解决方案1】:

    在类模板主体之外定义类模板的成员函数时,需要指定模板详情。

    对于您的情况,您需要使用:

    template< class Tkey, class Tvalue> // Missing from your code.
    boost::unordered_map<TKey,  TValue>::iterator CMyUnorderedMap< TKey,  TValue >::Begin()
    {
        return   m_myMap.begin()
    }
    

    【讨论】:

    • @R Sahu 我在类模板的主体之外指定了类模板,仍然存在编译错误
    • @Ushus,请发布Minimal, Complete, and Verifiable example,以及编译器错误消息。通过 cmets 交谈是没有效率的。
    【解决方案2】:

    在我更改代码后它工作了

    class CMyUnorderedMap
    {
    
       boost::Unordered_map<TKey,  TValue> m_myMap;
    public:
     typename boost::unordered_map<TKey,  TValue>::iterator Begin();
    
    };
    
    template< class Tkey, class Tvalue> 
    

    在函数的声明和定义中添加了关键字typename

    typename boost::unordered_map<TKey,  TValue>::iterator CMyUnorderedMap< TKey,  TValue >::Begin()
    
    {
    
        return   m_myMap.begin()
    } 
    

    【讨论】:

      猜你喜欢
      • 2023-02-21
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      相关资源
      最近更新 更多