在C++编译isues,试图通过调用另一个对象中的成员函数来创建一个std ::线程

问题描述:

WRT下面的代码,我发现编译isues,而试图创建一个线程通过调用另一个对象中的成员函数。在C++编译isues,试图通过调用另一个对象中的成员函数来创建一个std ::线程

th = std::thread(&AbcSFriend::S2F,this,friendObj); 

是导致编译错误的罪魁祸首。如果我删除这条线我编译罚款。

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 Visual 工作室12.0 \ VC \包括\官能(1149):error C2664:'eUserErrorCode std :: _ Pmf_wrap :: operator()(_ Farg0 &,Abc *)const':无法将参数2从'AbcSFriend'转换为'Abc *'1>
with 1> 1> _Farg0 = AbcSFriend 1>] 1> 没有可执行此 转换的用户定义转换运算符,或者不能调用运算符1> C:\ PROGRAM 文件(x86)\微软的Visual Studio 12.0 \ VC \包括\功能(1137): 见参考函数模板实例化“_UserErrorCode 的std :: _绑定,美国广播公司 *,AbcSFriend> :: _ Do_call <, 0x00,0x01>(std :: tuple <>,std :: _ Arg_idx < 0x00,0x01>)' 正在编译1> C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ VC \ include \ functional(1137 ):见参考起作用 模板实例“_UserErrorCode 的std :: _绑定,美国广播公司 *,AbcSFriend> :: _ Do_call <,0x00,0x01>(标准::元组<>,的std :: _ Arg_idx < 0x00,0x01>) ' 正在编译1> C:\ Pr文件(x86)\ Microsoft Visual Studio 12.0 \ VC \ include \ thr/xthread(195):请参阅参考函数 template instantiation'_UserErrorCode std :: _ Bind,Abc *,AbcSFriend> :: operator()< >(void)'正在编译1> C:\ Program Files文件(x86)\ Microsoft Visual Studio 12.0 \ VC \ include \ thr/xthread(195):请参阅函数模板实例的引用'_UserErrorCode std :: _ Bind,Abc编译类模板时,编译类模板时会出现以下错误信息:(1)编译类模板时,成员函数'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):参见 对函数模板实例的引用'unsigned int std :: _ LaunchPad < _Target> :: _ Run(std :: _ LaunchPad < _Target> *)'正在编译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 \包括\螺纹(49):见 参考起作用模板实例 '无效 的std :: _启动,美国广播公司 *,AbcSFriend >>(_ Thrd_t *,_目标& &)' 被编译1> 1> [ 1>
_Target = std :: _ Bind,Abc *,AbcSFriend> 1>] 1> .... \ Sources \ SOMEPLACESource.cpp(254):请参阅参考函数 模板实例化std :: thread ::螺纹(_Fn & &,美国广播公司常量 & &,AbcSFriend &)”被编译1> 1> [1>
_Fn = eUserErrorCode(__cdecl AbcSFr IEND ::
)(ABC *)

+2

我想你搞砸了参数,尝试'的std ::线程(AbcSFriend :: S2F,与friendObj,这一点);' – VTT

frinedObj(在AbcSFriend frinedObj)

不是

friendObj(在TH =标准::螺纹(& AbcSFriend :: S2F,为此, friendObj))。

修复拼写。

我认为正确的参数顺序是:

th = std::thread(&AbcSFriend::S2F, &friendObj, this); 

这也是情理之中的逆转成员th的声明的顺序和friendObj因为目前friendObjth之前销毁留下开放使用friendObjth的可能性破坏后friendObj。先声明friendObj,然后声明th

由于 '马克西姆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); 
    } 
};