Invocation with boost :: function f2 отлично работает, но подобный вызов для boost f3 не компилируется. Функция fun3, на которую указывает f3, содержит два аргумента std :: ostream и const char *, которые не компилируются. Может кто-то, пожалуйста, помогите мне разобраться, что я делаю неправильно здесь?Ошибка при компиляции Boost.Bind
1>Compiling...
1>BostFunction.cpp
1>c:\libraries\boost_1_57_0\boost\bind\bind.hpp(69) : error C2825: 'F': must be a class or namespace when followed by '::'
1> c:\libraries\boost_1_57_0\boost\bind\bind_template.hpp(15) : see reference to class template instantiation 'boost::_bi::result_traits<R,F>' being compiled
1> with
1> [
1> R=boost::_bi::unspecified,
1> F=void (__thiscall World::*)(std::ostream &,const char *)
1> ]
1> d:\vs projects\boost\bostfunction\bostfunction\bostfunction.cpp(34) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1> with
1> [
1> R=boost::_bi::unspecified,
1> F=void (__thiscall World::*)(std::ostream &,const char *),
1> L=boost::_bi::list2<boost::_bi::value<World *>,boost::arg<1>>
1> ]
1>c:\libraries\boost_1_57_0\boost\bind\bind.hpp(69) : error C2039: 'result_type' : is not a member of '`global namespace''
1>c:\libraries\boost_1_57_0\boost\bind\bind.hpp(69) : error C2146: syntax error : missing ';' before identifier 'type'
1>c:\libraries\boost_1_57_0\boost\bind\bind.hpp(69) : error C2208: 'boost::_bi::type' : no members defined using this type
1>c:\libraries\boost_1_57_0\boost\bind\bind.hpp(69) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://d:\VS Projects\Boost\BostFunction\BostFunction\Debug\BuildLog.htm"
class World
{
public:
void test() {
void fun2(int i) { cout << i << endl; }
void fun3(std::ostream &os, const char* str)
{
os << str << endl;
}
boost::function<void (int)> f2(boost::bind(&World::fun2, this, _1)); // Works Fine
boost::function<void (std::ostream &os, const char* str)> f3(boost::bind(&World::fun3, this, _1)); // How to make this work?
f2(111);
f3(boost::ref(std::cout), "Hello World");
}
};
int main()
{
boost::function<void(World*, std::ostream&, const char*)>f2 = &World::fun3;
World w;
f2(&w, boost::ref(std::cout), "Hello World"); // Works Fine
World w;
w.test();
}
Спасибо Эван. Извините, код не синхронизировался из-за копирования. Я попробовал изменения, которые вы предложили, и он выглядит нормально. Кстати, после более подробного анализа я попробовал следующий код и он тоже работал отлично. Я добавил дополнительный аргумент _2 в проблемную строку. :: function f3 (boost :: bind (& World :: fun3, this, _1, _2)); –
CodeBeginner