2

Take a look at these 2 codes.

The code below works fine.

void someFunction () {

    // Some unimportant stuff
}

MainM::MainM(QObject *parent) :
    QObject(parent)
{

    std::thread oUpdate (someFunction);

}

This code throws an error:

void MainM::someFunction () {      //as a class member


}


MainM::MainM(QObject *parent) :
    QObject(parent)
{

    std::thread oUpdate (someFunction);

}

Error:

error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>)'
     std::thread oUpdate (someFunction);
                                     ^
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
khajvah
  • 4,889
  • 9
  • 41
  • 63
  • possible duplicate of [Start thread with member function](http://stackoverflow.com/questions/10673585/start-thread-with-member-function) - did you even try searching for an existing answer? – Jonathan Wakely Sep 18 '13 at 19:52
  • @JonathanWakely It is, sorry. It is not ONLY std::thread related, so... – khajvah Sep 18 '13 at 20:08

1 Answers1

8

You can't create a pointer-to-member-function by applying & to just the name. You need the fully qualified member: &MainM::someFunction.

And also bind it to an instance, by passing this, e.g.

#include <thread>

struct MainM
{
    void someFunction() {
    }

    void main() 
    {
        std::thread th(&MainM::someFunction, this);
    }
};

int main()
{
    MainM m;
    m.main();
}
sehe
  • 374,641
  • 47
  • 450
  • 633