C++ 编译Boost.Bind时出错

C++ 编译Boost.Bind时出错,c++,boost,C++,Boost,使用boost::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 namespac

使用boost::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();
}
1>编译。。。
1> BostFunction.cpp
1> c:\libraries\boost\u 1\u 57\u 0\boost\bind\bind.hpp(69):错误C2825:“F”:后跟“:”时必须是类或命名空间
1> c:\libraries\boost\u 1\u 57\u 0\boost\bind\bind\u template.hpp(15):请参阅正在编译的类模板实例化“boost::\u bi::result\u traits”
1> 与
1>        [
1> R=增压::bi::未指定,
1> F=void(uu thiscall World::*)(std::ostream&,const char*)
1>        ]
1> d:\vs projects\boost\bostfunction\bostfunction\bostfunction.cpp(34):请参阅正在编译的类模板实例化“boost::\u bi::bind\u t”的参考
1> 与
1>        [
1> R=增压::bi::未指定,
1> F=void(uuu thiscall World::*)(std::ostream&,const char*),
1> L=boost::_bi::列表2
1>        ]
1> c:\libraries\boost\u 1\u 57\u 0\boost\bind\bind.hpp(69):错误C2039:“结果类型”:不是“全局命名空间”的成员
1> c:\libraries\boost\u 1\u 57\u 0\boost\bind\bind.hpp(69):错误C2146:语法错误:缺少“;”在标识符“type”之前
1> c:\libraries\boost\u 1\u 57\u 0\boost\bind\bind.hpp(69):错误C2208:“boost::\u bi::type”:没有使用此类型定义的成员
1> c:\libraries\boost\u 1\u 57\u 0\boost\bind\bind.hpp(69):致命错误C1903:无法从以前的错误中恢复;停止编译
1> 生成日志保存在“文件://d:\VS Projects\Boost\BostFunction\BostFunction\Debug\BuildLog.htm”
阶级世界
{
公众:
无效测试(){

voidfun2(inti){cout您的boost::函数类型规范对于f3是错误的

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

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

如果您愿意,您还可以将auto与
boost::bind
一起使用,而不是lambda(为了清晰和链接要求,不推荐使用),如果您愿意,您可以将
std::function
与lambda一起使用(声明方式与您对
boost::bind

的声明方式相同,谢谢Evan。抱歉,由于复制粘贴,代码变得不同步。我尝试了您建议的更改,看起来效果很好。顺便说一句,经过进一步分析,我尝试了以下代码,效果也很好。我在有问题的行中添加了一个附加参数_2)。boost::函数f3(boost::bind(&World::fun3,this,_1,_2));
boost::function<void (std::ostream &, const char*)> f3;
boost::function<void(World*, std::ostream&, const char*)>f2 = &World::Hello;
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");
    }
};