2010-11-18 10 views
0

MSVC++ 2008 Express Edition sürümünde anlayamadığım hatalar alıyorum. Birincisi, benim kod versiyonun (gerekirse göz ardı dahil) gösteririz: Ben olsunŞablon sınıfı MSVC++ içinde hata veren şablon sınıfı yuvaya yerleştirildi

template <class Key,class Value> 
class BinaryTree 
{ 
public: 
typedef std::pair<Key&,Value&> Element; 

... 

protected: 
template <bool _Reverse> 
class Iterator : public std::iterator<std::bidirectional_iterator_tag,Element> 
{ 
public: 
    Iterator(const Iterator<_Reverse>& other); 

    Iterator& operator++(); 

... 

protected: 
    typedef BinaryTree<Key,Value> Parent; 

    Iterator(Parent* parent,bool isEnd = false); 
} 
} 

... 

//Definition bodies (in the same file) 

... 

template <class Key,class Value,bool _Reverse> //line 118 
inline BinaryTree<Key,Value>::Iterator<_Reverse>::Iterator(Parent* parent,bool isEnd = false) 
//has an initialisation list (removing it didn't make a difference to the errors) 
{ 
... 
} //line 126 

... 

template <class Key,class Value,bool _Reverse> 
inline BinaryTree<Key,Value>::Iterator<_Reverse>::Iterator(const Iterator<_Reverse>& other) 
: _stack(other._stack), _parent(other._parent), _null(other._null) 
{ 
} //line 132 

... 

//the next two are specialisations 
template <class Key,class Value> 
typename BinaryTree<Key,Value>::Iterator<false>& BinaryTree<Key,Value>::Iterator<false>::operator++() 
{ 
... 
} //line 196 


template <class Key,class Value> 
typename BinaryTree<Key,Value>::Iterator<true>& BinaryTree<Key,Value>::Iterator<false>::operator++() 
{ 
... 
} //line 211 

Ve hataları şunlardır:

Bildiğim kadarıyla söyleyebilirim
//normal constructor 
binarytree.h(126) : error C3860: template argument list following class template name must list parameters in the order used in template parameter list 
binarytree.h(126) : error C3855: 'BinaryTree<Key,Value>::Iterator<_Reverse>': template parameter '_Reverse' is incompatible with the declaration 
binarytree.h(126) : error C2977: 'BinaryTree<Key,Value>::Iterator<_Reverse>' : too many template arguments 
binarytree.h(118) : error C2952: 'BinaryTree<Key,Value>::Iterator<_Reverse>::Iterator' : template declaration missing template parameter list 

//copy constructor 
binarytree.h(132) : error C2244: 'BinaryTree<Key,Value>::Iterator<_Reverse>::{ctor}' : unable to match function definition to an existing declaration 
     definition 
     'BinaryTree<Key,Value>::Iterator<_Reverse>::Iterator(const BinaryTree<Key,Value>::Iterator<_Reverse> &)' 
     existing declarations 
     'BinaryTree<Key,Value>::Iterator<_Reverse>::Iterator(BinaryTree<Key,Value> *,bool)' 
     'BinaryTree<Key,Value>::Iterator<_Reverse>::Iterator(const BinaryTree<Key,Value>::Iterator<_Reverse> &)'  //isn't this one clearly identical? 

//operator++ - template specialisations 
binarytree.h(196) : error C2244: 'BinaryTree<Key,Value>::Iterator<_Reverse>::operator ++' : unable to match function definition to an existing declaration 
     definition 
     'BinaryTree<Key,Value>::[email protected][email protected] &BinaryTree<Key,Value>::Iterator<false>::operator ++(void)' 
     existing declarations 
     'BinaryTree<Key,Value>::Iterator<_Reverse> BinaryTree<Key,Value>::Iterator<_Reverse>::operator ++(int)' 
     'BinaryTree<Key,Value>::Iterator<_Reverse> &BinaryTree<Key,Value>::Iterator<_Reverse>::operator ++(void)' 

binarytree.h(211) : error C2244: 'BinaryTree<Key,Value>::Iterator<_Reverse>::operator ++' : unable to match function definition to an existing declaration 
     definition 
     'BinaryTree<Key,Value>::[email protected]$00 &BinaryTree<Key,Value>::Iterator<true>::operator ++(void)' 
     existing declarations 
     'BinaryTree<Key,Value>::Iterator<_Reverse> BinaryTree<Key,Value>::Iterator<_Reverse>::operator ++(int)' 
     'BinaryTree<Key,Value>::Iterator<_Reverse> &BinaryTree<Key,Value>::Iterator<_Reverse>::operator ++(void)' 

, her üye fonksiyonu Bu üç hata grubundan birine sahiptir.

Bu sorunların tümünü, Yineleyicinin şablonlu paramater'ını kaldırarak, gerekli işlevleri türeyen ve geçersiz kılan ikinci bir sınıf olan ReverseIterator'ı oluşturarak çözebilirim. Ama neyin yanlış gittiğini anlamama yardımcı olmak için bu şekilde düzeltmeyi tercih ederim.

DÜZENLEME: Birisi kod etiketlerini düzeltebilir mi? Beklediğim gibi çalışmıyor gibi gözüküyor.

EDIT2: Tamam, ikinci şablon yönergesi kusursuz çalıştı. Ancak şablon uzmanlık hatalarının ortaya çıktığı görülmektedir, çünkü dış sınıfı uzmanlaşmamışsanız bir iç sınıfı uzmanlaşmanıza izin verilmez. Bu konuda çalışabilirim, fakat derleyiciyi belirli şeyleri en iyi duruma getirmeye güveniyorum. Yerine yazma Örneğin:

template <class Key,class Value> 
template <bool Reverse> 
inline bool BinaryTree<Key,Value>::Iterator<Reverse>::DoStuff() 
{ 
    if(Reverse) //hopefully is optimised out; at compile time is either true or false 
    { 
     return TrueCode(); 
    } 
    return FalseCode(); 
} 

Ben yardım edemem ama daha karmaşık durumlar derleyici bir gereksiz dalı atlayarak değil neden olabileceğini düşünmek:

template <class Key,class Value> 
inline bool BinaryTree<Key,Value>::Iterator<false>::DoStuff() 
{ 
    return FalseCode(); 
} 

template <class Key,class Value> 
inline bool BinaryTree<Key,Value>::Iterator<true>::DoStuff() 
{ 
    return TrueCode(); 
} 

ben kullandım. Ancak bu derleyiciye bağlı.

template <class Key,class Value> 
template <bool _Reverse> 
inline BinaryTree<Key,Value>::Iterator<_Reverse>::Iterator(const Iterator<_Reverse>& other) 
: _stack(other._stack), _parent(other._parent), _null(other._null) 
{ 
} //line 132 

ama çizgi ile başlayan bir değişkenler genellikle derleyici'nın uygulanması için ayrılmış olduğunu not alın: Bu iki bağımsız şablon sınıfları olduğundan

+0

Birisi biçimlendirmeyi düzeltmek için sorunuzu düzenledi, neden okunamaz hale getirmek için onu yeniden düzenlerdiniz? =/ – icecrime

+0

@icecream., Belki aynı zamanda düzenlediler, son kazananlar kazanır mı? –

+0

Evet, üzgünüm, geri aldım. – Innominate

cevap

1

, bu gibi örneğin olmalıdır.