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);

4
  • 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. Commented Aug 14, 2015 at 18:12
  • LibCurlWrapper *curlWrapper = new LibCurlWrapper; Why is that nonsense? Commented Aug 14, 2015 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 Commented Aug 14, 2015 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: stackoverflow.com/questions/10673585/… Commented Aug 14, 2015 at 18:31

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

When I try what you have suggested I get the following compiler error. error: no type named 'type' in 'class std::result_of<std::_Mem_fn<int (LibCurlWrapper::*)(const std::basic_string<char>&, const std::basic_string<char>&, const std::basic_string<char>&, const std::vector<unsigned char>&)>(std::basic_string<char>, std::basic_string<char>, std::basic_string<char>, std::vector<unsigned char>)>' typedef typename result_of<_Callable(_Args...)>::type result_type; ^
@nateN What compiler are you using? I tried clang, it compiles fine. (Although got a link error.) rextester.com/YYSX61209
ahh...missed one detail...forgot to add the second parameter curlWrapper. Now it works! Thank you
@nateN Glad to have been of help! Feel free to accept my answer if you feel it was useful to you. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.