编译问题 C++ 同时,尝试通过调用另一个对象中的成员函数来创建 std:: 线程
Compilation issues in C++ while, trying to create a std:: thread by calling a member function in another object
WRT 在代码下方,我发现编译问题,同时尝试通过在另一个对象中调用成员函数来创建线程。
th = std::thread(&AbcSFriend::S2F,this,friendObj);
是导致以下编译错误的罪魁祸首。如果我删除此行,iit 编译正常。
class AbcSFriend
{
public:
void S2F(Abc* ptr)
{}
};
class Abc
{
public:
std::thread th;
AbcSFriend frinedObj;
void FL()
{
th = std::thread(&AbcSFriend::S2F,this,friendObj);
}
};
UDT 时无法生成复制或复制分配运算符 包含一个零大小的数组 1>C:\程序文件 (x86(\Microsoft 可视 Studio 12.0\VC\include\functional(1149(:错误 C2664:"eUserErrorCode std::_Pmf_wrap::运算符 (((_Farg0 &,Abc *( const' :无法将参数 2 从 'AbcSFriend' 转换为 'Abc *' 1
>与 1>[ 1>_Farg0=AbcSFriend 1>] 1> 没有可用的用户定义转换运算符可以执行此操作 转换,否则运算符不能调用 1>C:\Program 文件 (x86(\Microsoft Visual Studio 12.0\VC\include\functional(1137( : 请参阅对函数模板实例化的引用 '_UserErrorCode 标准::_Bind,ABC *,AbcSFriend>::_Do_call<,0x00,0x01>(std::tuple<>,std::_Arg_idx<0x00,0x01>(' 正在编译 1>C:\程序文件 (x86(\Microsoft 可视 Studio 12.0\VC\include\functional(1137(:参见函数参考 模板实例化 '_UserErrorCode 标准::_Bind,ABC *,AbcSFriend>::_Do_call<,0x00,0x01>(std::tuple<>,std::_Arg_idx<0x00,0x01>(' 正在编译 1>C:\程序文件 (x86(\Microsoft 可视 Studio 12.0\VC\include\thr/xthread(195( : 参见函数参考 模板实例化 '_UserErrorCode 标准::_Bind,ABC *,AbcSFriend>::运算符 ((<>(void(' 正在编译 1>C:\Program Files (x86(\Microsoft Visual Studio 12.0\VC\include\thr/xthread(195(:请参阅对函数模板实例化的引用_UserErrorCode 标准::_Bind,ABC *,AbcSFriend>::运算符 ((<>(void(' 正在编译 1>C:\Program Files (x86(\Microsoft Visual Studio 12.0\VC\include\thr/xthread(192(:编译类模板成员函数'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *(' 1>with 1>
[ 1>
_Target=std::_Bind,abc *,AbcSFriend> 1>] 1>C:\Program Files (x86(\Microsoft Visual Studio 12.0\VC\include\thr/xthread(187( : 参见 对函数模板实例化"无符号 int "的引用 std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *(' 编译 1>with 1>[ 1>
_Target=std::_Bind,abc *,AbcSFriend> 1>] 1>C:\Program Files (x86(\Microsoft Visual Studio 12.0\VC\include\thr/xthread(205( : 参见 对类模板实例化"std::_LaunchPad<_Target>"的引用 编译 1>与 1>[ 1>
_Target=std::_Bind,abc *,AbcSFriend> 1>] 1>C:\Program Files (x86(\Microsoft Visual Studio 12.0\VC\include\thread(49( : 参见 对函数模板实例化 'void 的引用 标准::_Launch,ABC *,AbcSFriend>>(_Thrd_t *,_Target &&(' 正在编译 1>与 1>[ 1>
_Target=std::_Bind,abc *,abcSFriend> 1>] 1>....\Sources\SOMEPLACESource.cpp(254( : 参见函数参考 模板实例化 'std::thread::thread(_Fn &&,Abcconst &&,AbcSFriend &(' 正在编译 1>with 1>[ 1>
_Fn=eUserErrorCode (__cdecl AbcSFriend::((abc *(
frinedObj
(在AbcSFriend frinedObj中(
莫
friendObj
(in th = std::thread(&AbcSFriend::S2F,this,friendObj(;).
修正拼写。
正如">Maxim Egorushkin"所说,尝试:
class AbcSFriend
{
public:
void S2F(class Abc* ptr);
};
class Abc
{
public:
std::thread th;
AbcSFriend friendObj;
void FL()
{
th = std::thread(&AbcSFriend::S2F, &friendObj, this);
}
};
我认为正确的参数顺序是:
th = std::thread(&AbcSFriend::S2F, &friendObj, this);
颠倒成员声明的顺序也是有意义的th
和friendObj
因为目前friendObj
销毁之前th
留下了th
使用friendObj
的可能性,friendObj
销毁之后。首先声明friendObj
,最后声明th
。
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 将公共但非静态的成员函数与ALGLIB集成
- 使用指向成员的指针将成员函数作为参数传递
- 将重载的成员函数传递给函数模板
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何在C++中使用非静态成员函数作为回调函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 关联容器的下界复杂性:成员函数与非成员函数
- 在 C++ 中用派生类型重写成员函数
- 链表的泛型函数remove()与成员函数remove)
- 如何将lambda作为模板类的成员函数参数
- constexpr构造函数需要常量成员函数时出现问题
- 将自由函数绑定为类成员函数
- 区分非成员函数和头文件中的成员函数
- 如何从子成员函数修改父公共成员变量
- 保留对其他类的成员函数的引用
- 在运算符重载定义中使用成员函数(const错误)
- 内联如何影响模块接口中的成员函数
- 将成员函数指针作为参数传递给模板方法