【问题标题】:Why can't I store boost::function in std::list?为什么我不能将 boost::function 存储在 std::list 中?
【发布时间】:2008-11-11 11:29:17
【问题描述】:

我收到以下编译错误:

error: expected `;' before 'it'"

这是我的代码:

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

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

为什么会这样?我该如何解决?

【问题讨论】:

    标签: c++ list boost


    【解决方案1】:

    您需要将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++ 常见问题解答条目Where to put template and typename on dependent names

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-05
      • 2019-04-26
      • 2017-04-22
      • 2012-06-25
      • 1970-01-01
      相关资源
      最近更新 更多