C ++中的编译问题,同时尝试通过调用另一个对象

时间:2017-07-16 18:26:53

标签: c++ multithreading c++11 mutex stdthread

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时无法生成copy-ctor或copy-assignment运算符   包含零大小的数组1> C:\ Program Files(x86)\ Microsoft Visual   Studio 12.0 \ VC \ include \ functional(1149):错误C2664:' eUserErrorCode   std :: _ Pmf_wrap :: operator()(_ Farg0&,Abc *)const' :无法转换来自' AbcSFriend'到' Abc *' 1 GT;
  用1> [1> _Farg0 = AbcSFriend 1> ] 1>   没有可用于执行此操作的用户定义转换运算符   转换,或者操作员不能被称为1> C:\ PROGRAM   文件(x86)\ Microsoft Visual Studio 12.0 \ VC \ include \ functional(1137):   请参阅函数模板实例化' _UserErrorCode   的std :: _绑定,美国广播公司   *,AbcSFriend> :: _ Do_call<,0x00,0x01>(标准::元组<>,的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:\ Program Files(x86)\ Microsoft Visual   Studio 12.0 \ VC \ include \ thr / xthread(195):参见函数参考   模板实例化' _UserErrorCode   的std :: _绑定,美国广播公司   *,AbcSFriend> :: operator()<>(void)'正在编译1> C:\ Program Files(x86)\ Microsoft Visual Studio   12.0 \ VC \ include \ thr / xthread(195):参见函数模板实例化' _UserErrorCode   的std :: _绑定,美国广播公司   *,AbcSFriend> :: operator()<>(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 GT;
  用1> [1>   _target =的std :: _绑定,美国广播公司   *,AbcSFriend> 1 GT; ] 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> [1>   _target =的std :: _绑定,美国广播公司   *,AbcSFriend> 1 GT; ] 1> C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ VC \ include \ thr / xthread(205):请参阅   引用类模板实例化' std :: _ LaunchPad< _Target>'   正在编译1>用1> [1>   _target =的std :: _绑定,美国广播公司   *,AbcSFriend> 1 GT; ] 1> C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ VC \ include \ thread(49):参见   参考函数模板实例化' void   的std :: _启动,美国广播公司   *,AbcSFriend>>(_ Thrd_t *,_ Target&&)'正在编译1>用1> [1>   _target =的std :: _绑定,美国广播公司   *,AbcSFriend> 1 GT; ] 1> .... \ Sources \ SOMEPLACESource.cpp(254):参见函数参考   模板实例化&#st; :: thread :: thread(_Fn&&,Abc const   &&,AbcSFriend&)'正在编译1>用1> [1>   _Fn = eUserErrorCode(__ cdecl AbcSFriend :: )(Abc *)

3 个答案:

答案 0 :(得分:0)

frinedObj(在AbcSFriend frinedObj;)

不是

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

修复拼写。

答案 1 :(得分:0)

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

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

反转成员thfriendObj声明的顺序也是有意义的,因为当前friendObjth之前销毁th的可能性friendObjfriendObj被破坏后使用friendObj。首先声明th,最后声明var userSchema = new Schema({...}); userSchema.pre('save', function(next){ // tried with pre('validate') console.log('triggered...'); next(); }); var User = module.exports = mongoose.model('User', userSchema);

答案 2 :(得分:0)

正如' 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);
    }
};
相关问题