Problem about defining an iterator

Y

yatko

Hi all;

I want to write a template class that uses STL list class and behaves
as circular list. But when I try to define an iterator the compiler
gives the following error. I really don't understand what I am doing
wrong.

Thanks

yatko

Error:

Multiple markers at this line
- type 'std::list<CircularList<DataType>::Node,
std::allocator<CircularList<DataType>::Node> >' is not derived from
type 'CircularList<DataType>'
- expected ';' before 'popIterator'



template < class DataType>
class CircularList
{

public:
...

private:

struct Node
{
DataType *dataptr;
bool valid;
};


list< Node > linear;

list < Node >::iterator popIterator;

..

};
 
V

Victor Bazarov

yatko said:
Hi all;

I want to write a template class that uses STL list class and behaves
as circular list. But when I try to define an iterator the compiler
gives the following error. I really don't understand what I am doing
wrong.

Read up on dependent names.
[..]
list < Node >::iterator popIterator;

typename list said:

V
 
Y

yatko

One more thing, it seems that both list<Node> and list<Node>::iterator
are dependent-names. But why there is no any error, when I dont use
typename for the declaration of list<Node> linear.

Thanks

yatko
 
A

acehreli

One more thing, it seems that both list<Node> and list<Node>::iterator
are dependent-names. But why there is no any error, when I dont use
typename for the declaration of list<Node> linear.

list<Node> is the name of a type. That type is the result type of the
instantiation of the list class template.

list<T>::iterator depends on what T is: depending on whether list is
specialized for that type T, iterator may be a member object, an enum
value, a type, etc.

Ali
 
J

James Kanze

One more thing, it seems that both list<Node> and
list<Node>::iterator are dependent-names. But why there is no
any error, when I dont use typename for the declaration of
list<Node> linear.

Because the compiler knows that list<Node> is a type. The name
"list", by itself, is not dependent, and the compiler find the
class template at the definition point of the template, and
binds the name to it. And since it is a class template, all
specializations must be a class type.

What the compiler doesn't know is what might be in any
particular specialization. It can't know whether
list<Node>::iterator names a member type, or a member variable,
or a member function, or whatever. And for various
histerical^H^H^H^H^H^Horical reasons, it's impossible to
effectively parse C++ without knowing whether a symbol names a a
type or something else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top