0
votes

WRT below code, I'm finding compilation issues while, trying to create a thread by calling a member function in another object.

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

is the culprit line causing below compilation error. If i remove this line iit compiles fine.

class AbcSFriend
{
public:
    void S2F(Abc* ptr)
    {}
};

class Abc
{
public:
    std::thread th;
    AbcSFriend frinedObj;
    void FL()
    {
        th = std::thread(&AbcSFriend::S2F,this,friendObj);
    }
};

Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array 1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional(1149): error C2664: 'eUserErrorCode std::_Pmf_wrap::operator ()(_Farg0 &,Abc *) const' : cannot convert argument 2 from 'AbcSFriend' to 'Abc *' 1>
with 1> [ 1> _Farg0=AbcSFriend 1> ] 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional(1137) : see reference to function template instantiation '_UserErrorCode std::_Bind,Abc *,AbcSFriend>::_Do_call<,0x00,0x01>(std::tuple<>,std::_Arg_idx<0x00,0x01>)' being compiled 1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional(1137) : see reference to function template instantiation '_UserErrorCode std::_Bind,Abc *,AbcSFriend>::_Do_call<,0x00,0x01>(std::tuple<>,std::_Arg_idx<0x00,0x01>)' being compiled 1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\thr/xthread(195) : see reference to function template instantiation '_UserErrorCode std::_Bind,Abc *,AbcSFriend>::operator ()<>(void)' being compiled 1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\thr/xthread(195) : see reference to function template instantiation '_UserErrorCode std::_Bind,Abc *,AbcSFriend>::operator ()<>(void)' being compiled 1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\thr/xthread(192) : while compiling class template member function '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) : see reference to function template instantiation 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)' being compiled 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) : see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled 1> with 1> [ 1>
_Target=std::_Bind,Abc *,AbcSFriend> 1> ] 1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\thread(49) : see reference to function template instantiation 'void std::_Launch,Abc *,AbcSFriend>>(_Thrd_t *,_Target &&)' being compiled 1> with 1> [ 1>
_Target=std::_Bind,Abc *,AbcSFriend> 1> ] 1> ....\Sources\SOMEPLACESource.cpp(254) : see reference to function template instantiation 'std::thread::thread(_Fn &&,Abc const &&,AbcSFriend &)' being compiled 1> with 1> [ 1>
_Fn=eUserErrorCode (__cdecl AbcSFriend::
)(Abc *)

3
I think you messed up arguments, try std::thread(&AbcSFriend::S2F, &friendObj, this); - user7860670

3 Answers

0
votes

frinedObj (in AbcSFriend frinedObj;)

is not

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

Fix the spelling.

0
votes

As 'Maxim Egorushkin' says, try:

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);
    }
};
0
votes

I think the correct order of arguments is:

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

It also makes sense to reverse the order of the declaration of members th and friendObj because currently friendObj destroys before th leaving open a possibility of th using friendObj after friendObj destroyed. Declare friendObj first and th last.