[英]Error while compiling Boost.Bind
Invocation with boost::function f2 works fine, but the similar invocation for boost f3 is not compiling. 使用boost :: function f2调用可以正常工作,但是无法编译与boost f3类似的调用。 The function fun3 which f3 points to contains two arguments std::ostream and const char* which is not compiling.
f3指向的函数fun3包含两个未编译的参数std :: ostream和const char *。 Can someone please help me with figuring out what I am doing wrong here?
有人可以帮我弄清楚我在做什么错吗?
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();
}
your boost::function type specification is wrong for f3 您的boost :: function类型规范对于f3是错误的
fun2 takes an int parameter and returns void so you correctly declare fun2接受一个int参数并返回void,因此您可以正确声明
boost::function<void (int)> f2;
fun3 has a different parameter list so the boost function must be declared to match: fun3具有不同的参数列表,因此必须声明boost函数以匹配:
boost::function<void (std::ostream &, const char*)> f3;
I also don't understand this line: 我也不明白这一行:
boost::function<void(World*, std::ostream&, const char*)>f2 = &World::Hello;
because I don't see a member of World
named Hello
. 因为我看不到
World
的成员Hello
。
If you have access to c++11, I'd recommend using new features. 如果您可以使用c ++ 11,建议您使用新功能。 Boost::bind is entirely superceded by lambda and
std::function
or auto
can be used instead of boost::function
. Boost :: bind完全被lambda取代,并且
std::function
或auto
可以代替boost::function
来使用。
consider: 考虑:
class World
{
public:
void test() {
auto f2([](int i){ std::cout << i << std::endl; });
auto f3([](std::ostream &os, const char* str){ os << str << std::endl });
f2(111);
f3(boost::ref(std::cout), "Hello World");
}
};
You can also use auto with boost::bind
instead of the lambda if you wish (not recommended for clarity and link requirements) and you can use std::function
with lambda if you wish (declared the same way you would for boost::bind
. 您还可以使用与汽车
boost::bind
的,而不是拉姆达您是否希望(不建议清晰度和链接的要求),你可以使用std::function
,如果你想与拉姆达(声明你会为同样的方式boost::bind
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.