gpt4 book ai didi

c++ - 为什么我不能将 boost::function 存储在 std::list 中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:53:56 24 4
gpt4 key购买 nike

我得到以下编译错误:

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 on dependent names .

关于c++ - 为什么我不能将 boost::function 存储在 std::list 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/280654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com