5

I get the following compilation error:

error: expected `;' before 'it'"

Here's my code:

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

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

Why does this happen? How can I fix it?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Nayruden
  • 205
  • 3
  • 10

1 Answers1

18

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.


Also consult the new C++ FAQ entry Where to put template and typename on dependent names.

Community
  • 1
  • 1
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212