本文介绍了指向成员函数错误的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译以下代码时,出现以下错误.谁能帮助我解决这个问题.谢谢.

When I am compiling the following piece of code, I am getting the following error. Can anyone help me in resolving this issue. Thank you.

错误:ISO C ++禁止使用绑定的成员函数的地址形成指向成员函数的指针.说& foo :: abc" [-fpermissive]

boost :: thread testThread(boost :: bind(& f.abc,f));

............................................... ......................... ^

........................................................................^

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>

class foo
{
    private:

    public:
    foo(){}

    void abc()
    {
        std::cout << "abc" << std::endl;
    }
};

int main()
{
    foo f;

    boost::thread testThread(&f.abc, f);

    return 0;
}

推荐答案

该错误消息确实无法清除

The error message couldn't really be any clearer

boost::thread testThread(boost::bind(&foo::abc, f));
//                                   ^^^^^^^

此外,也不需要boost::bind,这也应该起作用

Also, there's no need for boost::bind, this should work too

boost::thread testThread(&foo::abc, f);

请注意,这两个副本均会复制f,如果要避免使用以下任何一种方法

Be aware that both of these make copies of f, if you want to avoid that you should use either of the following

testThread(&foo::abc, &f);
testThread(&foo::abc, boost::ref(f));


现在,为什么main()class zoo的成员函数?


Now, why on earth is main() a member function of class zoo??

这篇关于指向成员函数错误的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 03:58