我有一个小问题,我无法弄清楚为什么这段代码不起作用:
std::for_each(users.begin(), users.end(), [](Wt::WString u)
{
std::cout << "ilosc: " << users.size() << std::endl;
userBox_->addItem(u);
});
我在编译时遇到的错误:
GameWidget.cpp: In lambda function:
GameWidget.cpp:352:30: error: 'users' is not captured
GameWidget.cpp:353:4: error: 'this' was not captured for this lambda function
GameWidget.cpp: In member function 'virtual void GameWidget::updateUsers()':
GameWidget.cpp:354:3: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
GameWidget.cpp:354:4: error: no matching function for call to 'for_each(std::set<Wt::WString>::iterator, std::set<Wt::WString>::iterator, GameWidget::updateUsers()::<lambda(Wt::WString)>)'
GameWidget.cpp:354:4: note: candidate is:
In file included from /usr/include/c++/4.7/algorithm:63:0,
from GameWidget.h:11,
from GameWidget.cpp:9:
/usr/include/c++/4.7/bits/stl_algo.h:4436:5: note: template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)
GameWidget.cpp:354:4: error: template argument for 'template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)' uses local type 'GameWidget::updateUsers()::<lambda(Wt::WString)>'
GameWidget.cpp:354:4: error: trying to instantiate 'template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)'
我正在使用gcc 4.7.3
,所以我的编译器可能支持 C++11。
userBox_
是一个集合,BOOST_FOREACH
适用于此代码:
BOOST_FOREACH(Wt::WString i, users)
{
std::cout << "ilosc: " << users.size() << std::endl;
userBox_->addItem(i);
}
感谢您的任何回答,我很好奇为什么会这样。