【问题标题】:Template code wont compile with GCC模板代码不能用 GCC 编译
【发布时间】:2012-04-27 13:27:39
【问题描述】:

相关代码:

Line 476: 
typedef KeyWrapper < Key, gist_traits, KeyAllocator, key_policy > KeyWrapper;

Line 304: 
template<class Key, class GIST_TRAITS, class Allocator, class key_policy >
    struct KeyWrapper : public KeyWrapperImpl<Key, GIST_TRAITS, Allocator, typename         key_policy::type >
    {
        typedef KeyWrapperImpl<Key, GIST_TRAITS, Allocator, typename key_policy::type > base;
        typedef KeyWrapper<Key, GIST_TRAITS, Allocator, key_policy> this_type;
        KeyWrapper() {}
        ~KeyWrapper() {}
        explicit KeyWrapper(const Key& k): base(k) {}
        this_type& operator=(const KeyWithGist<Key, GIST_TRAITS, key_policy>& k)
        {
            base::operator=(k);
            return *this;
        }

    };
Line 477:
typedef KeyWithGist < Key, gist_traits, key_policy > KeyWithGist;


Line 194:
template<class Key, class GIST_TRAITS, class key_policy>
    struct KeyWithGist : public KeyWithGistImpl<Key, GIST_TRAITS, typename key_policy::type>
    {
        typedef KeyWithGistImpl<Key, GIST_TRAITS, typename key_policy::type> base;
        KeyWithGist(const Key& k) : base(k) {}
    };

Line 1307:
typedef iter<value_type> iterator;

Line 731:
typedef iter<value_type> iterator;

Line 1308:
typedef iter<const value_type> const_iterator;

Line 732:
typedef iter<const value_type> const_iterator;

Line 1309:
typedef typename boost::reverse_iterator<iterator> reverse_iterator;

Line 733:
typedef typename boost::reverse_iterator<iterator> reverse_iterator;

Line 1310:
typedef typename boost::reverse_iterator<const_iterator> const_reverse_iterator;

Line 734:
typedef typename boost::reverse_iterator<const_iterator> const_reverse_iterator;

错误:

stree.h|476|error: declaration of ‘typedef struct sti::implementation::KeyWrapper<Key, gist_traits, typename Allocator::rebind<Key>::other, key_policy> sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::KeyWrapper’ [-fpermissive]|

stree.h|304|error: changes meaning of ‘KeyWrapper’ from ‘struct sti::implementation::KeyWrapper<Key, gist_traits, typename Allocator::rebind<Key>::other, key_policy>’ [-fpermissive]|

stree.h|477|error: declaration of ‘typedef struct sti::implementation::KeyWithGist<Key, gist_traits, key_policy> sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::KeyWithGist’ [-fpermissive]|

stree.h|194|error: changes meaning of ‘KeyWithGist’ from ‘struct sti::implementation::KeyWithGist<Key, gist_traits, key_policy>’ [-fpermissive]|

stree.h|1307|error: declaration of ‘typedef class sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::iter<ValueType> sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::iterator’|

stree.h|731|error: conflicts with previous declaration ‘typedef class sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::iter<ValueType> sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::iterator’|

stree.h|1308|error: declaration of ‘typedef class sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::iter<const ValueType> sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::const_iterator’|

stree.h|732|error: conflicts with previous declaration ‘typedef class sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::iter<const ValueType> sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::const_iterator’|

stree.h|1309|error: declaration of ‘typedef class boost::reverse_iterator<sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::iter<ValueType> > sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::reverse_iterator’|

stree.h|733|error: conflicts with previous declaration ‘typedef class boost::reverse_iterator<sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::iter<ValueType> > sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::reverse_iterator’|

stree.h|1310|error: declaration of ‘typedef class boost::reverse_iterator<sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::iter<const ValueType> > sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::const_reverse_iterator’|

stree.h|734|error: conflicts with previous declaration ‘typedef class boost::reverse_iterator<sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::iter<const ValueType> > sti::implementation::stree<Key, ValueType, Allocator, comparator, key_extractor, BN, key_policy, value_policy, gist_traits>::const_reverse_iterator’|

||=== Build finished: 12 errors, 0 warnings ===|

注意事项: 我正在尝试构建发布在 codeproject 上的地图版本,但它似乎无法使用 GCC 编译。谁能解释一下为什么下面的代码不能在 GCC 上编译?

http://www.codeproject.com/Articles/27799/Stree-A-fast-std-map-and-std-set-replacement

【问题讨论】:

  • 编译器在错误信息中非常清楚,恕我直言。您正在重新声明一个名称。您有什么不清楚的地方吗?
  • “为什么下面的代码不能在 GCC 上编译”——因为它坏了?

标签: c++ templates map


【解决方案1】:

您不能使用typedef 重新定义已定义的类型名。它实际上与模板没有任何关系。下面的代码也有同样的问题:

typedef float MyType;
typedef int MyType;

typeToy.cpp:4:13: error: conflicting declaration ‘typedef int MyType’
typeToy.cpp:3:15: error: ‘MyType’ has a previous declaration as ‘typedef float MyType’

为您的 typedef 目标选择一个不同于现有名称的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多