1

I am trying to invoke a function from a new thread. The function takes a bunch of parameters.

std::thread signingThread(curlWrapper->SendDataToServer,username, password, serverURL, data);

Here is the function protoype

int LibCurlWrapper::SendDataToServer(const std::string& username, const std::string& password, const std::string& serverURL, const std::vector<unsigned char> &vData)

Compiler Error

error: no matching function for call to 'std::thread::thread(, std::string&, std::string&, std::string&, std::vector&)' std::thread signingThread(curlWrapper->SendDataToServer,username, password, serverURL, data);

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
nateN
  • 29
  • 1
  • `curlWrapper->SendDataToServer` is nonsense, that's not valid C++. There are **hundreds** of questions on StackOverflow about launching a `std::thread` to run a member function. – Jonathan Wakely Aug 14 '15 at 18:12
  • LibCurlWrapper *curlWrapper = new LibCurlWrapper; Why is that nonsense? – nateN Aug 14 '15 at 18:14
  • @Jonathan Wakely Also, I have searched a lot before posting this...but nothing that I saw or read has worked. If you have a link please do share. Thnx – nateN Aug 14 '15 at 18:22
  • Because you can't refer to a member function like that. You can use `curlWrapper->SendDataToServer` in a function call if you follow it with an argument list, but you didn't do that. You need to construct the thread with a pointer-to-member-function that refers to the member function and with a pointer/reference to the object to call the function on. Here's one example of an existing question: http://stackoverflow.com/questions/10673585/start-thread-with-member-function/10673671#10673671 – Jonathan Wakely Aug 14 '15 at 18:31

1 Answers1

2

You need to specify a function to std::thread, while curlWrapper->SendDataToServer doesn't and couldn't. (Although curlWrapper->SendDataToServer() means call SendDataToServer() on curlWrapper.)

Change it to:

std::thread signingThread(&LibCurlWrapper::SendDataToServer, curlWrapper, username, password, serverURL, data);
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • When I try what you have suggested I get the following compiler error. error: no type named 'type' in 'class std::result_of&, const std::basic_string&, const std::basic_string&, const std::vector&)>(std::basic_string, std::basic_string, std::basic_string, std::vector)>' typedef typename result_of<_Callable(_Args...)>::type result_type; ^ – nateN Aug 14 '15 at 18:19
  • 1
    @nateN What compiler are you using? I tried clang, it compiles fine. (Although got a link error.) http://rextester.com/YYSX61209 – songyuanyao Aug 14 '15 at 18:30
  • ahh...missed one detail...forgot to add the second parameter curlWrapper. Now it works! Thank you – nateN Aug 14 '15 at 18:32
  • @nateN Glad to have been of help! Feel free to [accept my answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if you feel it was useful to you. :-) – songyuanyao Aug 14 '15 at 18:44