所有,

在myclass.cpp中

MyClass::Initialize()
{
    m_thread = new std::thread( &Foo::func, *this );
}

在foo.cpp中:
void Foo::func(MyClass &obj)
{
    // some processing
    // which involves modifying `obj`
}

我在gcc上收到编译器错误:
error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Foo::*)(MyClass&)>(Foo*, MyClass)>'
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^

TIA!

最佳答案

为了调用Foo::func,它需要一个Foo类型的对象来调用它。因此,您必须问自己,func实际上是否需要成为成员函数,或者是非静态成员函数?如果您要使用Foo对象来调用它,则可以将其作为第二个参数传递。

至于第三个,您将传递*this,但是由于std::thread会复制其每个参数,因此您将需要使用引用包装并以这种方式传递它:

m_thread = new std::thread( &Foo::func, foo, std::ref(*this) );

09-10 07:55
查看更多