我收到以下编译错误:
error: expected `;' before 'it'"
这是我的代码:
#include <boost/function.hpp>
#include <list>
template< class T >
void example() {
std::list< boost::function<T ()> >::iterator it;
}
为什么会这样?我该如何解决?
您需要放在typename
该行的前面,因为您执行 ::iterator 的类型取决于模板参数 T。像这样:
template< class T >
void example() {
typename std::list< boost::function<T ()> >::iterator it;
}
考虑线
std::list< boost::function<T ()> >::iterator * it;
这可能意味着乘法或指针。这就是为什么你需要typename
明确你的意图。没有它,编译器就不会假定类型,因此它需要一个运算符或语法上的分号。
另请参阅新的 C++ FAQ 条目Where to put template and typename ondependent names。