指向成员函数的指针错误
Pointer to member function error
当我编译以下代码时,我收到以下错误。谁能帮我解决这个问题。谢谢。
错误:ISO C++禁止获取绑定成员函数的地址来形成指向成员函数的指针。 说 '&foo::abc' [-fallowive]
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;
}
错误消息再清楚
不过
了说 '&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();
}
直播: http://rextester.com/XDE37483
相关文章:
- 为什么我在空指针错误(链表)中获取成员访问权限
- std::函数指针错误:无法将 &A::a 转换为类型 std::function<>&&
- 为什么我得到 munmap_chunk():无效的指针:错误
- 成员模板函数指针错误
- 删除指针时,未分配被释放的 C++ 指针错误
- Visual Studio 中的"指向引用的指针"错误不会按发生位置的文件名显示
- C++数组指针错误无法将“int*”转换为“int**”
- C++ 通过函数指针错误调用成员函数
- 从 BST 中删除元素(指针错误)
- 随机生成的多维布尔指针错误
- 在测试中使用unique_ptr时指针错误无效
- 使用 malloc 而不是 new 会导致 free():指针错误无效
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 抽象类和唯一指针 c++ 错误
- 返回函数指针错误而不带类型定义
- 练习中出现空指针错误
- 指针错误导致的分段错误(简单..)
- free():C++中的指针错误无效
- 使用布尔值数组"表达式必须具有指向对象类型的指针"错误C++
- 指针错误的C 数组