编译问题 C++ 同时,尝试通过调用另一个对象中的成员函数来创建 std:: 线程

Compilation issues in C++ while, trying to create a std:: thread by calling a member function in another object

本文关键字:成员 函数 创建 线程 std 一个对象 同时 C++ 问题 调用 编译      更新时间:2023-10-16

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);

颠倒成员声明的顺序也是有意义的thfriendObj因为目前friendObj销毁之前th留下了th使用friendObj的可能性,friendObj销毁之后。首先声明friendObj,最后声明th