8

I am trying to create a thread using std::async, but I keep getting the error "no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::string&)’" on the line

ConnectFuture = std::async(std::launch::async, Connect_T,ip);

Here is the code that produces this behaviour:

#include <future>

class libWrapper
{
public:

    void Connect(std::string ip);
    void Connect_T(std::string ip);

private:

    std::future<void> ConnectFuture;
};



void libWrapper::Connect(std::string ip){

    auto status = ConnectFuture.wait_for(std::chrono::seconds(0));
    if (status != std::future_status::timeout)
    {
        ConnectFuture = std::async(std::launch::async, Connect_T,ip);

    }
}

void libWrapper::Connect_T(std::string ip)
{


}

int main(int argc, char** argv) {
    libWrapper lW;
    lW.Connect("192.168.3.1");
    return 0;
}
Casey
  • 41,449
  • 7
  • 95
  • 125
Andres La
  • 306
  • 1
  • 3
  • 9
  • Do you really get ``? I get `reference to non-static member function must be called` from clang and `invalid use of non-static member function` from gcc, both of which make more sense. – Barry May 12 '15 at 16:12
  • yes, here is my compiler call: g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/test2.d" -MT"src/test2.d" -o "src/test2.o" "../src/test2.cpp" – Andres La May 12 '15 at 16:44

1 Answers1

16

It's a member function, so it needs an object to be called on as well as an argument. Maybe it should be static, or maybe you should bind it to this:

std::async(std::launch::async, &libWrapper::Connect_T, this, ip)
Barry
  • 286,269
  • 29
  • 621
  • 977
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644