Yet again I find myself struggling with the C++ syntax.
I'm trying to iterate over a list of generic objects. That is I have objects of a class
Event<Q>
, crammed into a std::list<Event<Q> >
.
So I'm trying to get an iterator over the list and intuitively thought that
std::list<Event<Q> >::iterator it;
for (it = events.begin(); it != events.end(); it++) { ... }
should solve the problem. However, i keep getting these errors:
..\calendar.h:48: error: expected `;' before "it"
..\calendar.h:49: error: `it' was not declared in this scope
Is there a reason for it being this difficult?
Sure this should work, but it sounds like you either have one or both of the following in action.
Q
a template parameter or a type that somehow otherwise depend on it (typedef to it). Put a typename
before std::list
then, so that the compiler knows that ::iterator
is a type and can proceed analysis properly (it could be a static value member). Knowing it is a type, the compiler can produce better diagnostics for template definitions, and you are required to tell whether it is a type using typename
. events
a const list. Use const_iterator
then. Update: The shown compile error surely indicates that the first point is true: Q
directly or indirectly depends on a template parameter, and you have to put typename
like so:
typename std::list< Event<Q> >::iterator it;
The compiler believes you that it is a non-type, and so it requires some operator or semicolon behind ::iterator
(like, a multiplication). Put typename
to make it know it names a type instead.
Update: See the Template FAQ for similar template issues and solutions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With