gpt4 book ai didi

c++ - 编译 Boost.Bind 时出错

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:50:30 24 4
gpt4 key购买 nike

用 boost::function f2 调用工作正常,但对 boost f3 的类似调用没有编译。 f3 指向的函数 fun3 包含两个未编译的参数 std::ostream 和 const char* 。有人可以帮我弄清楚我在这里做错了什么吗?

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();
}

最佳答案

你的 boost::function 类型规范对于 f3 是错误的

fun2 接受一个 int 参数并返回 void,因此您可以正确声明

boost::function<void (int)> f2;

fun3 具有不同的参数列表,因此必须声明 boost 函数以匹配:

boost::function<void (std::ostream &, const char*)> f3;

我也不明白这一行:

boost::function<void(World*, std::ostream&, const char*)>f2 = &World::Hello;

因为我没有看到名为 HelloWorld 成员。

建议的更改

如果您可以访问 c++11,我建议您使用新功能。 Boost::bind 完全被 lambda 取代,并且可以使用 std::functionauto 代替 boost::function

考虑:

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");
}
};

如果您愿意,您也可以将 auto 与 boost::bind 一起使用而不是 lambda(出于清晰度和链接要求不推荐),并且您可以使用 std::function 如果你愿意,可以使用 lambda(声明方式与 boost::bind 相同。

关于c++ - 编译 Boost.Bind 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29878327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com