简体   繁体   中英

C++: iterating over a list of a generic type

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.

  • Have 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 .
  • Have 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.

in your sample , what is S ?

list<Event<S> >::iterator it; // what is S 
for (it = events.begin(); it != events.end(); it++) { ... }

can you try putting a native type there ?

list<Event<int> >::iterator it; 
for (it = events.begin(); it != events.end(); it++) { ... }

or simplify it even more ?

list<int>::iterator it; 
for (it = events.begin(); it != events.end(); it++) { ... }

Have you got the std namespace imported? Should the iterator declaration have been:

std::list<Event<S> >::iterator it;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM