指向成员函数错误的指针
Pointer to member function error
当我编译以下代码时,出现以下错误。谁能帮我解决这个问题。谢谢。
错误: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;
}
错误信息再清楚不过了
Say ‘&foo::abc’
boost::thread testThread(boost::bind(&foo::abc, f));
// ^^^^^^^
另外,不需要boost::bind
,这个应该也可以
boost::thread testThread(&foo::abc, f);
请注意,这两者都会复制 f
,如果您想避免使用以下任一方法
testThread(&foo::abc, &f);
testThread(&foo::abc, boost::ref(f));
现在,为什么 main()
是 class zoo
的成员函数??
按照错误提示,将f.abc
替换为foo::abc
:
boost::thread testThread(boost::bind(&foo::abc, f));
使用:
boost::thread testThread(boost::bind(&foo::abc, f));
照@Praetorian 说的去做。一切正常:
//Title of this code
//Compiler Version 18.00.21005.1 for x86
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
class zoo;
class foo
{
private:
public:
foo(){}
void abc()
{
std::cout << "abc" << std::endl;
}
};
class zoo
{
public:
int main()
{
foo f;
boost::thread testThread(boost::bind(&foo::abc, &f));
testThread.join();
return 0;
}
};
int main()
{
zoo{}.main();
}
当我编译以下代码时,出现以下错误。谁能帮我解决这个问题。谢谢。
错误: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;
}
错误信息再清楚不过了
Say ‘&foo::abc’
boost::thread testThread(boost::bind(&foo::abc, f));
// ^^^^^^^
另外,不需要boost::bind
,这个应该也可以
boost::thread testThread(&foo::abc, f);
请注意,这两者都会复制 f
,如果您想避免使用以下任一方法
testThread(&foo::abc, &f);
testThread(&foo::abc, boost::ref(f));
现在,为什么 main()
是 class zoo
的成员函数??
按照错误提示,将f.abc
替换为foo::abc
:
boost::thread testThread(boost::bind(&foo::abc, f));
使用:
boost::thread testThread(boost::bind(&foo::abc, f));
照@Praetorian 说的去做。一切正常:
//Title of this code
//Compiler Version 18.00.21005.1 for x86
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
class zoo;
class foo
{
private:
public:
foo(){}
void abc()
{
std::cout << "abc" << std::endl;
}
};
class zoo
{
public:
int main()
{
foo f;
boost::thread testThread(boost::bind(&foo::abc, &f));
testThread.join();
return 0;
}
};
int main()
{
zoo{}.main();
}