簡體   English   中英

類型名C ++錯誤

[英]Typename C++ error

抱歉,這個標題含糊不清,很難描述。

我遇到的錯誤是這樣,我不知道這意味着什么:

carray.h:176:錯誤:'類型名Carray <T,Allocator> :: is_iterator'名稱'template <class T,Class Allocator> template <class I,bool check>結構Carray <T,Allocator> :: is_iterator',這不是一種類型

我有這個代碼片段來檢測某個東西是否是一個迭代器,並使用正確的重載(http://stackoverflow.com/questions/6050441/why-does-this-constructor-overload-resolve-incorrect)。 這樣編譯:

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。

對於這些以某種方式難以理解的問題(即,有很多代碼,而且在第一遍中閱讀起來並不簡單),您應該提供一個可行的(或者是失敗的示例)。

我的猜測是您缺少template關鍵字( Carray構造函數參數):

typename Carray<T, Allocator>::template is_iterator<InputIterator>::type
//                             ^^^^^^^^

在每一行的末尾檢查分號,看起來您已經寫了以下內容:

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

第一行末沒有分號,只是一個猜測。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM