請看看下面簡單的例子:幫我打的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