tags:

views:

593

answers:

1

Why does the following code have the error "error: expected `;' before 'it'"?

#include <boost/function.hpp>
#include <list>

template< class T >
void example() {
    std::list< boost::function<T ()> >::iterator it;
}
+12  A: 

You need to put typename in front of that line, since the type you do ::iterator upon is dependant on the template-parameter T. Like this:

template< class T >
void example() {
    typename std::list< boost::function<T ()> >::iterator it;
}

Consider the line

std::list< boost::function<T ()> >::iterator * it;

which could mean a multiplication, or a pointer. That's why you need typename to make your intention clear. Without it, the compiler assumes not a type, and thus it requires an operator there or a semicolon syntactically.

Johannes Schaub - litb