编译Boost.Bind时出错

时间:2015-04-26 13:35:46

标签: c++ boost

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

1 个答案:

答案 0 :(得分:0)

你的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;

因为我没有看到名为World的{​​{1}}成员。

建议更改

如果您有权访问c ++ 11,我建议您使用新功能。 Boost :: bind完全被lambda取代,Hellostd::function可以代替auto使用。

考虑:

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"); } }; 代替lambda(不建议用于清晰度和链接要求),如果您愿意,可以将boost::bind与lambda一起使用(以与您相同的方式声明)会std::function