C++ 为什么可以';我在std::list中存储boost::函数吗?

C++ 为什么可以';我在std::list中存储boost::函数吗?,c++,list,boost,C++,List,Boost,我发现以下编译错误: error: expected `;' before 'it'" 这是我的密码: #include <boost/function.hpp> #include <list> template< class T > void example() { std::list< boost::function<T ()> >::iterator it; } #包括 #包括 模板 void示例(){ std::l

我发现以下编译错误:

error: expected `;' before 'it'"
这是我的密码:

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

template< class T >
void example() {
    std::list< boost::function<T ()> >::iterator it;
}
#包括
#包括
模板
void示例(){
std::list::迭代器;
}

为什么会发生这种情况?如何修复它?

您需要将
typename
放在该行前面,因为您使用的类型::iterator取决于模板参数T。如下所示:

template< class T >
void example() {
    typename std::list< boost::function<T ()> >::iterator it;
}
模板
void示例(){
typename std::list::迭代器;
}
想想这条线

std::list< boost::function<T ()> >::iterator * it; 
std::list::迭代器*it;
这可能意味着乘法或指针。这就是为什么您需要
typename
来明确您的意图。如果没有它,编译器将假定不是类型,因此它需要一个运算符或语法上的分号


也参考新的C++ FAQ条目。< /P>