请看看下面简单的例子:帮我打的boost ::拉姆达::表达if_then的调用一个函数对象
#include <vector>
#include <string>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/if.hpp>
using namespace boost::lambda;
namespace bl = boost::lambda;
using namespace std;
struct Item
{
string sachNr;
int ist;
int soll;
};
struct Printer
{
void operator()(const Item &item)
{
m_erg += item.sachNr;
}
operator string()
{
return m_erg;
}
private:
string m_erg;
};
void TestFunction()
{
vector<Item> pickItems;
string result = for_each(pickItems.begin(), pickItems.end(),
bl::if_then(bl::bind(&Item::ist, bl::_1) == bl::bind(&Item::soll, bl::_1),
Printer()));
}
ERRORMESSAGE(GCC)
/TestCpp-build-desktop/../TestCpp/ctest.cpp:52: error: no matching function for call to ‘if_then(const boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::relational_action<boost::lambda::equal_action>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<2, boost::lambda::function_action<2, boost::lambda::detail::unspecified> >, boost::tuples::tuple<int Item::* const, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<2, boost::lambda::function_action<2, boost::lambda::detail::unspecified> >, boost::tuples::tuple<int Item::* const, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, Printer)’
任何提示什么是错的用那个代码?
它应该调用任何item.soll == item.ist项目的打印机仿函数。
感谢您的帮助!
不知道这特别的错误,而是试图用替代语法(`if_(条件)[功能]`揭示了另一个问题:对函子'的for_each `不是'Printer',而是一个boost lambda对象,因此你不能指望字符串的转换发生 - 因为你已经写了一个函数,为什么不让它做更多的工作? – visitor 2010-11-29 14:06:04