Sorry for the long headline.
I have a typedef in the List class:
template <typename T> class List {
and definition outside the class, but inside the header file.
template <typename T> typename List<T>::const_iterator List<T>::cbegin() const {}
This causes error C2373: Redefinition; different type modifiers Redefinition; different type modifiers
I rewrote this function:
template <typename T> const typename List<T>::Iter_ List<T>::cbegin() const {}
and the error has disappeared; The program compiles correctly. (think that I will not return anything in these examples, this is not relevant to this example.)
What is a compiler interpreter with an erroneous version that prevents successful compilation, which is not in the second version, and how can I fix it?
More code
I am using VS2008
Example code (more complete) that I am programming now:
template <typename T> class List { public: // Forward declaration. class Iter_; private: ///////////////////////////////////////////////////////// // ListElem ///////////////////////////////////////////////////////// struct ListElem { T data; // Doubly-linked list. ListElem *next; ListElem *prev; }; class ListException {}; //////////////////////////////////////////////////////// // List Members //////////////////////////////////////////////////////// // Point to first elem. ListElem *head_; // Point to last elem. ListElem *tail_; public: ////////////////////////////////////////////////////////// // Typedefs ////////////////////////////////////////////////////////// typedef Iter_ iterator; typedef const Iter_ const_iterator; ////////////////////////////////////////////////////////// // Iterator class ////////////////////////////////////////////////////////// class Iter_ { public: Iter_( ListElem *pCurr, List *pList ) : pCurr_(pCurr), pList_(pList) { } T& operator*() { if( *this == pList_->end() ) throw ListException(); else return pCurr_->data; } private: ListElem *pCurr_; List *pList_; }; iterator begin(); iterator end(); const_iterator cbegin() const; const_iterator cend() const; }; template <typename T> List<T>::List() : head_(0), tail_(0), size_(0) { } template <typename T> List<T>::~List() { //this->clear(); } template <typename T> List<T>::List( List const& other ) : size_(other.size_) { //this->clone(other); } template <typename T> List<T>& List<T>::operator=( List const& other ) { size_ = other.size_; //this->clone(other); } // Compiles ok template <typename T> typename List<T>::iterator List<T>::begin() { if(!head_) head_ = new ListElem(); return iterator(head_, this); } // Compiles ok template <typename T> typename List<T>::iterator List<T>::end() { return iterator(tail_, this); } // Compiler error template <typename T> typename List<T>::const_iterator List<T>::cbegin() const { return const_iterator(head_, this); } // Compiles ok template <typename T> typename const List<T>::Iter_ List<T>::cend() const { return const_iterator(tail_, this); }
source share