1

对不起,这个非常模糊的标题,很难描述。

我遇到的错误是这个,我不知道这意味着什么:

carray.h:176: 错误: 'typename Carray<T, Allocator>::is_iterator' 名称 'template<class T, class Allocator> template<class I, bool check> struct Carray<T, Allocator>::is_iterator',这不是一种类型

我有这个片段来检测某些东西是否是迭代器并使用正确的重载(http://stackoverflow.com/questions/6050441/why-does-this-constructor-overload-resolve-incorrectly)。这编译:

template<class T> class Carray {
private:
    // uses SFINAE to determine if the passed in type is indeed an iterator
    template<class It>
    class is_iterator_impl {
    private:
        typedef char yes[1];
        typedef char no[2];

        template<class C>
        static yes& _test(typename C::iterator_category*);

        template<class>
        static no& _test(...);
    public:
        static const bool value = sizeof(_test<It>(0)) == sizeof(yes);
    };

    template<class It, bool check = is_iterator_impl<It>::value> struct is_iterator { typedef void type; };
    template<class It> struct is_iterator<It*, false> { typedef void type; };
    template<class It> struct is_iterator<It, false> { };

public:
    template<class It>
    Carray(It first, It last, typename is_iterator<It>::type *dummy = 0) {
        // create array from 2 iterators
    }
};

现在我想将实现与声明分开,我尝试了这个,但我得到了错误:

template<class T> class Carray {
private:
    // uses SFINAE to determine if the passed in type is indeed an iterator
    template<class It> class is_iterator_impl;
    template<class It, bool check = is_iterator_impl<It>::value> struct is_iterator { typedef void type; };
    template<class It> struct is_iterator<It*, false> { typedef void type; };
    template<class It> struct is_iterator<It, false> { };
public:
    template<class It> Carray(It first, It last, typename is_iterator<It>::type *dummy = 0);
};

template<class T>
template<class It>
Carray<T>::Carray(It first, It last, typename Carray<T>::is_iterator<It>::type *dummy) {
    // create array from 2 iterators - ERROR IN THIS DEFINITION
}

template<class T>
template<class It>
class Carray<T>::is_iterator_impl {
private:
    typedef char yes[1];
    typedef char no[2];

    template<class C>
    static yes& _test(typename C::iterator_category*);

    template<class>
    static no& _test(...);
public:
    static const bool value = sizeof(_test<It>(0)) == sizeof(yes);
};

我正在使用 g++ 4.5.5。

4

2 回答 2

3

对于这些在某种程度上晦涩难懂的问题(即有相当多的代码,并且在第一遍中阅读并不简单),您应该提供一个有效的(或者更确切地说是失败的示例)。

我的猜测是您缺少template关键字(Carray构造函数参数):

typename Carray<T, Allocator>::template is_iterator<InputIterator>::type
//                             ^^^^^^^^
于 2011-06-18T17:52:05.130 回答
-2

检查每一行末尾的分号,看起来你写了这样的东西:

template<class T, class Allocator> 
template<class I, bool check> struct Carray<T, Allocator>::is_iterator

第一行末尾没有分号,只是一个猜测。

于 2011-06-18T17:35:53.073 回答