我想获得相同的行为,因为这:如何用C++ 03中的成员函数返回的值填充向量?
IdentifiersGenerator gen;
for(int i = 0; i < 100; ++i)
v.push_back(gen.getNextIdentifiers());
具有类似的语法:
IdentifiersGenerator gen;
std::vector<Identifiers> v(100);
std::generate(v.begin(), v.end(),
std::bind1st(std::mem_fun(&IdentifiersGenerator::getNextIdentifiers), gen));
上面的代码提供了以下错误:
test/src/IdentifiersGeneratorTest.cpp:449: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:100: error: no type named ‘second_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:103: error: no type named ‘first_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:106: error: no type named ‘first_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:111: error: no type named ‘second_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:117: error: no type named ‘second_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h: In function ‘std::binder1st<_Operation> std::bind1st(const _Operation&, const _Tp&) [with _Operation = std::mem_fun_t<const Identifiers&, IdentifiersGenerator>, _Tp = IdentifiersGenerator]’:
test/src/IdentifiersGeneratorTest.cpp:449: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:126: error: no type named ‘first_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
或许这能够即使没有std::vector
默认对象初始化使用例如boost::make_function_input_iterator
。
EDIT行,所以我已经意识到,我可以使用boost::bind
像这样:
std::vector<AlarmIdentifiers> v(100);
std::generate(v.begin(), v.end(),
boost::bind(&IdentifiersGenerator::getNextIdentifiers, &gen));
任何人都可以共享的方法来初始化指针成员函数,而不是构建载体中,然后在其内部产生?
是否好做一个仿函数? – NathanOliver
不,我需要成员函数'IdentifiersGenerator :: getNextIdentifiers' – Patryk
你可以使用函数来获得它。我在问你是否可以用一个仿函数包装这个电话。 – NathanOliver