S
Shriramana Sharma
Hello. Please consider the following minimized example for the problem I amfacing.
template < typename T >
struct item {
typedef item<T> * pointer ;
pointer prev, next ;
} ;
template < typename ItemPtr >
struct iterator_base {
public:
iterator_base () : ptr(0) {}
protected:
ItemPtr ptr ;
explicit iterator_base ( ItemPtr p ) : ptr(p) {}
} ;
template < typename T >
struct iterator : public iterator_base < item<T> * > {
template<typename> friend class const_iterator ;
} ;
template < typename T >
struct const_iterator : public iterator_base < const item<T> * > {
const_iterator () {}
const_iterator ( const iterator<T> & it ) : ptr(it.ptr) {}
} ;
int main () {
iterator<int> it ;
const_iterator<int> cit ;
cit = it ;
}
<<<------------------------
I get the error messages:
$ g++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp
iterator-base-minimal-2.cpp: In constructor ‘const_iterator<T>::const_iterator(const iterator<T>&)’:
iterator-base-minimal-2.cpp:28:46: error: class ‘const_iterator<T>’ does not have any field named ‘ptr’
const_iterator ( const iterator<T> & it ) : ptr(it.ptr) {}
^
$ clang++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp
iterator-base-minimal-2.cpp:28:46: error: member initializer 'ptr' does notname a non-static data member or base class
const_iterator ( const iterator<T> & it ) : ptr(it.ptr) {}
^~~~~~~~~~~
1 error generated.
<<<-------------------------------
I am not sure why I can't access ptr which is a protected member of a parent class. OK so anyhow I thought I'll explicitly scope it by the base type and so I changed the const_iterator definition to:
template < typename T >
struct const_iterator : public iterator_base < const item<T> * > {
typedef iterator_base < const item<T> * > base ;
const_iterator () {}
const_iterator ( const iterator<T> & it ) : base:tr(it.ptr) {}
} ;
<<<-------------------------------
But now I get even more cryptic error messages:
$ g++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp
iterator-base-minimal-2.cpp: In instantiation of ‘const_iterator<T>::const_iterator(const iterator<T>&) [with T = int]’:
iterator-base-minimal-2.cpp:35:6: required from here
iterator-base-minimal-2.cpp:28:62: error: no type named ‘ptr’ in ‘struct iterator_base<const item<int>*>’
const_iterator ( const iterator<T> & it ) : base:tr(it.ptr) {}
^
$ clang++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp
iterator-base-minimal-2.cpp:28:52: error: typename specifier refers to non-type member 'ptr' in 'iterator_base<const item<int> *>'
const_iterator ( const iterator<T> & it ) : base:tr(it.ptr) {}
^~~
iterator-base-minimal-2.cpp:35:8: note: in instantiation of member function'const_iterator<int>::const_iterator' requested here
cit = it ;
^
iterator-base-minimal-2.cpp:13:10: note: referenced member 'ptr' is declared here
ItemPtr ptr ;
^
iterator-base-minimal-2.cpp:28:52: error: 'ptr' is a protected member of 'iterator_base<const item<int> *>'
const_iterator ( const iterator<T> & it ) : base:tr(it.ptr) {}
^
iterator-base-minimal-2.cpp:13:10: note: must name member using the type ofthe current context 'const_iterator<int>'
ItemPtr ptr ;
^
2 errors generated.
<<<-------------------------------
I'm using GCC 4.8.1 and Clang 3.2 on Kubuntu Raring 64-bit. Can anyone please tell me what the problem is and how I can solve it? Thank you very much!
template < typename T >
struct item {
typedef item<T> * pointer ;
pointer prev, next ;
} ;
template < typename ItemPtr >
struct iterator_base {
public:
iterator_base () : ptr(0) {}
protected:
ItemPtr ptr ;
explicit iterator_base ( ItemPtr p ) : ptr(p) {}
} ;
template < typename T >
struct iterator : public iterator_base < item<T> * > {
template<typename> friend class const_iterator ;
} ;
template < typename T >
struct const_iterator : public iterator_base < const item<T> * > {
const_iterator () {}
const_iterator ( const iterator<T> & it ) : ptr(it.ptr) {}
} ;
int main () {
iterator<int> it ;
const_iterator<int> cit ;
cit = it ;
}
<<<------------------------
I get the error messages:
$ g++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp
iterator-base-minimal-2.cpp: In constructor ‘const_iterator<T>::const_iterator(const iterator<T>&)’:
iterator-base-minimal-2.cpp:28:46: error: class ‘const_iterator<T>’ does not have any field named ‘ptr’
const_iterator ( const iterator<T> & it ) : ptr(it.ptr) {}
^
$ clang++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp
iterator-base-minimal-2.cpp:28:46: error: member initializer 'ptr' does notname a non-static data member or base class
const_iterator ( const iterator<T> & it ) : ptr(it.ptr) {}
^~~~~~~~~~~
1 error generated.
<<<-------------------------------
I am not sure why I can't access ptr which is a protected member of a parent class. OK so anyhow I thought I'll explicitly scope it by the base type and so I changed the const_iterator definition to:
template < typename T >
struct const_iterator : public iterator_base < const item<T> * > {
typedef iterator_base < const item<T> * > base ;
const_iterator () {}
const_iterator ( const iterator<T> & it ) : base:tr(it.ptr) {}
} ;
<<<-------------------------------
But now I get even more cryptic error messages:
$ g++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp
iterator-base-minimal-2.cpp: In instantiation of ‘const_iterator<T>::const_iterator(const iterator<T>&) [with T = int]’:
iterator-base-minimal-2.cpp:35:6: required from here
iterator-base-minimal-2.cpp:28:62: error: no type named ‘ptr’ in ‘struct iterator_base<const item<int>*>’
const_iterator ( const iterator<T> & it ) : base:tr(it.ptr) {}
^
$ clang++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp
iterator-base-minimal-2.cpp:28:52: error: typename specifier refers to non-type member 'ptr' in 'iterator_base<const item<int> *>'
const_iterator ( const iterator<T> & it ) : base:tr(it.ptr) {}
^~~
iterator-base-minimal-2.cpp:35:8: note: in instantiation of member function'const_iterator<int>::const_iterator' requested here
cit = it ;
^
iterator-base-minimal-2.cpp:13:10: note: referenced member 'ptr' is declared here
ItemPtr ptr ;
^
iterator-base-minimal-2.cpp:28:52: error: 'ptr' is a protected member of 'iterator_base<const item<int> *>'
const_iterator ( const iterator<T> & it ) : base:tr(it.ptr) {}
^
iterator-base-minimal-2.cpp:13:10: note: must name member using the type ofthe current context 'const_iterator<int>'
ItemPtr ptr ;
^
2 errors generated.
<<<-------------------------------
I'm using GCC 4.8.1 and Clang 3.2 on Kubuntu Raring 64-bit. Can anyone please tell me what the problem is and how I can solve it? Thank you very much!